2012-01-29 10 views
1

各セクションのキーを含む3つのNSArraysから3つのセクションを取り出すUITableViewをセットアップしました。各セルのdetailTextLabelsとアクセサリビューを手動で設定するswitch文を設定しようとしましたが、テーブルビューをスクロールするたびにセルが一見して変わってしまいます。これを行うより効率的な方法はありますか?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

+0

何が起きるかは、スクロールしていくと、いくつかのセルからのアクセサリビューと、他人の詳細テキストラベルがすべての行に表示されるようになります。 [indexPath section]は全体的なセクションを表すのではなく、表示されたセクションを表しているようですが、少なくともそれは私にとって意味のあるものです。 – gurooj

+0

あなたのセルは再利用されているため、以前は有効な設定が有効になっていますか?セルを初期化するときに表示または非表示にする必要があるものはすべてリセットする必要があります。 – Till

答えて

0

の内側にあったが、 @ Tillのコメントに全面的に感謝します。すでに存在するセルを再利用することにより、UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]の作業が行われるようです。したがって、テーブル内で複数のセルタイプを使用するには、異なる再利用識別子を使用する必要があります。それとも、私が唯一の7つのまたは8テーブルビューセルを持っているので、私はちょうど私がUISwitchを含まないようにしたいすべてのセルのnilにセルアクセサリビューを設定し、そのよう:

[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; 
} 

このように私に迷惑を保存しました複数の再利用識別子を扱う

関連する問題