2012-01-29 77 views
1

我已經建立了一個UITableView的3個部分,從3個NSArrays拉包含每個部分的關鍵。我嘗試設置一個switch語句,爲每個單元格手動設置detailTextLabels和附件視圖,但每次滾動表格視圖時,單元格看起來都會自行更改。有沒有更有效的方法來做到這一點?DetailTextLabel不斷變化的UITableViewCell

這是一個方法,我嘗試使用:

switch (indexPath.section) 
{ 
    // info 
    case 0:{ 
     cell.textLabel.text = [infoKeysArray objectAtIndex:indexPath.row]; 
     if ([cell.textLabel.text isEqualToString:@"Duration"]) 
     { 
      cell.detailTextLabel.text = [task stringForDuration]; 
     } 
     if ([cell.textLabel.text isEqualToString:@"Elapsed"]) 
     { 
      // elapsed time 
     } 
     if ([cell.textLabel.text isEqualToString:@"Today"]) 
     { 
      UISwitch *todaySwitch = [[UISwitch alloc] initWithFrame:CGRectZero]; 
      [todaySwitch addTarget:self action:@selector(todaySwitchValueChanged:) forControlEvents:UIControlEventValueChanged]; 
      [todaySwitch setOn:task.isToday]; 
      cell.accessoryView = todaySwitch; 
      [todaySwitch release]; 
     } 
    } 
     break; 
    // actions 
    case 1:{ 
     cell.textLabel.text = [actionsKeysArray objectAtIndex:indexPath.row]; 
     if ([cell.textLabel.text isEqualToString:@"Priority"]) 
     { 
      // priority 
      [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
     } 
     if ([cell.textLabel.text isEqualToString:@"Finish Early"]) 
     { 
      // finish early 
     } 
     if ([cell.textLabel.text isEqualToString:@"Set New Time"]) 
     { 
      [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
     } 
     if ([cell.textLabel.text isEqualToString:@"Repeating"]) 
     { 
      UISwitch *repeatingSwitch = [[UISwitch alloc] initWithFrame:CGRectZero]; 
      [repeatingSwitch addTarget:self action:@selector(repeatingSwitchValueChanged:) forControlEvents:UIControlEventValueChanged]; 
      [repeatingSwitch setOn:task.isRepeating]; 
      cell.accessoryView = repeatingSwitch; 
      [repeatingSwitch release]; 
      //cell.detailTextLabel.text = nil; 
     } 
    } 
     break; 
    // details 
    case 2:{ 
     cell.textLabel.text = [detailsKeysArray objectAtIndex:indexPath.row]; 
     if ([cell.textLabel.text isEqualToString:@"Specifics"]) 
     { 
      cell.detailTextLabel.text = task.specifics; 
      [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
     } 
     if ([cell.textLabel.text isEqualToString:@"Repeated"]) 
     { 
      // times repeated 
     } 
    } 
     break; 
    default: 
     break; 
} 

這是另一種:

// Info section 
if ([indexPath section] == 0) 
{ 
    // duration 
    cell.textLabel.text = [infoKeysArray objectAtIndex:indexPath.row]; 
    if (indexPath.row == 0) 
    { 
     cell.detailTextLabel.text = [task stringForDuration]; 
    } 
    // elapsed time 
    if (indexPath.row == 1) 
    { 
     //cell.detailTextLabel.text = [task stringForTotalElapsedTime]; 
    } 
    // today status 
    if (indexPath.row == 2) 
    { 
     UISwitch *todaySwitch = [[UISwitch alloc] initWithFrame:CGRectZero]; 
     [todaySwitch addTarget:self action:@selector(todaySwitchValueChanged:) forControlEvents:UIControlEventValueChanged]; 
     [todaySwitch setOn:task.isToday]; 
     cell.accessoryView = todaySwitch; 
     [todaySwitch release]; 

    } 
} 
// actions section 
if ([indexPath section] == 1) 
{ 
    cell.textLabel.text = [actionsKeysArray objectAtIndex:indexPath.row]; 
    // priority 
    if (indexPath.row == 0) 
    { 
     [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
    } 
    // finish early 
    if (indexPath.row == 1) 
    { 
     //[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
    } 
    // set new time 
    if (indexPath.row == 2) 
    { 
     //cell.accessoryView = repeatingSwitch; 
     [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
    } 
    // repeating 
    if (indexPath.row == 3) 
    { 
     UISwitch *repeatingSwitch = [[UISwitch alloc] initWithFrame:CGRectZero]; 
     [repeatingSwitch addTarget:self action:@selector(repeatingSwitchValueChanged:) forControlEvents:UIControlEventValueChanged]; 
     [repeatingSwitch setOn:task.isRepeating]; 
     cell.accessoryView = repeatingSwitch; 
     [repeatingSwitch release]; 
     cell.detailTextLabel.text = nil; 
    } 
} 
// details section 
if ([indexPath section] == 2) 
{ 
    cell.textLabel.text = [detailsKeysArray objectAtIndex:indexPath.row]; 
    // specifics 
    if (indexPath.row == 0) 
    { 
     //cell.detailTextLabel.text = taskDetails.specifics; 
     cell.detailTextLabel.text = task.specifics; 
     [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
    } 
    // repeated 
    if (indexPath.row == 1) 
    { 
     [cell setAccessoryView:nil]; 
    } 
} 

這兩種嘗試都內cellForRowAtIndexPath

+0

你應該引用你最相關的代碼。 – Till 2012-01-29 02:26:04

+0

最終發生的事情是,我滾動的越多,則來自某些單元格的附件視圖和來自其他單元格的詳細文本標籤就會開始出現在每一行中,即使它開始沒問題。看起來[indexPath部分]不代表整體部分,而是代表顯示的部分,至少這是唯一對我有意義的事情。 – gurooj 2012-01-29 03:34:11

+0

您是否知道您的細胞已被重複使用,因此以前的有效設置仍處於活動狀態?初始化單元格時,必須完全重置應顯示或隱藏的任何內容。 – Till 2012-01-29 03:36:26

回答

0

我最終解決這個問題我自己,完全感謝@ Till的評論。看起來UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]的工作方式是重用已經存在的單元格,因此爲了在表格中使用多個單元格類型,必須使用不同的重用標識符。或者,因爲我只有7個或8表視圖細胞,我剛纔設置的細胞附件以零對,我想不包含UISwitch,像這樣的所有單元格:

[cell setAccessoryType:UITableViewCellAccessoryNone]; 

switch ([indexPath section]) 
{ 
    case 0: 
    { 
     cell.textLabel.text = [infoKeysArray objectAtIndex:indexPath.row]; 
     switch (indexPath.row) { 
      case 0: 
      { 
       cell.detailTextLabel.text = task.stringForDuration; 
       [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
       cell.accessoryView = nil; 
      } 
       break; 
      case 1: 
      { 
       cell.detailTextLabel.text = @"00:00"; 
       [cell setAccessoryType:UITableViewCellAccessoryNone]; 
       cell.accessoryView = nil; 
      } 
       break; 
      case 2: 
      { 
       UISwitch *todaySwitch = [[UISwitch alloc] initWithFrame:CGRectZero]; 
       [todaySwitch addTarget:self action:@selector(todaySwitchValueChanged:) forControlEvents:UIControlEventValueChanged]; 
       [todaySwitch setOn:task.isToday]; 
       cell.accessoryView = todaySwitch; 
       [todaySwitch release]; 
       [cell setAccessoryType:UITableViewCellAccessoryNone]; 
       cell.detailTextLabel.text = @""; 
      } 
       break; 
      default: 
       break; 
     } 
    } 
     break; 
    case 1: 
    { 
     cell.textLabel.text = [actionsKeysArray objectAtIndex:indexPath.row]; 
     switch (indexPath.row) { 
      case 0: 
      { 
       cell.detailTextLabel.text = @"Low"; 
       [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
       cell.accessoryView = nil; 
      } 
       break; 
      case 1: 
      { 
       cell.detailTextLabel.text = @""; 
       [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
       cell.accessoryView = nil; 
      } 
       break; 
      case 2: 
      { 
       UISwitch *repeatingSwitch = [[UISwitch alloc] initWithFrame:CGRectZero]; 
       [repeatingSwitch addTarget:self action:@selector(repeatingSwitchValueChanged:) forControlEvents:UIControlEventValueChanged]; 
       [repeatingSwitch setOn:task.isRepeating]; 
       cell.accessoryView = repeatingSwitch; 
       [repeatingSwitch release]; 
       cell.detailTextLabel.text = @""; 
       [cell setAccessoryType:UITableViewCellAccessoryNone]; 
      } 
       break; 
      default: 
       break; 
     } 
    } 
     break; 
    case 2: 
    { 
     cell.textLabel.text = [detailsKeysArray objectAtIndex:indexPath.row]; 
     switch (indexPath.row) { 
      case 0: 
      { 
       cell.detailTextLabel.text = task.specifics; 
       [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
       cell.accessoryView = nil; 
      } 
       break; 
      case 1: 
      { 
       cell.detailTextLabel.text = @"0"; 
       [cell setAccessoryType:UITableViewCellAccessoryNone]; 
       cell.accessoryView = nil; 
      } 
       break; 
      default: 
       break; 
     } 
     break; 
    } 
    default: 
     break; 
} 

這樣救了我的麻煩處理多個重用標識符。