2010-12-15 15 views
1

2つのセクションを含むUITableViewを作成しています。テーブルが最初にロードされると、すべてのセルに正しい情報が表示されますが、上下スクロールを開始すると、detailedTextLabelセルとaccessoryTypeセルが正しくリフレッシュされないため、detailedTextLabelだけを含むセルにアクセサリも含まれます。アクセサリだけを含む必要があり、detailedTextLabelも含みます。スクロール時にUITableViewが正しく更新されない

内部cellForRowAtIndexPath:ネストされたswitch/case文を使用して、それぞれのセクション/行のセルに正しい値を適用しています。限り、私はこれらのステートメントのロジックが正しいことを伝えることができるので、cell変数の値が更新時にinccorectである可能性はありますか?

テーブルが正しく読み込まれますが、スクロール後にaccessoryTypeとdetailedTextLabelが混在します。ここで

Click for link to screen shots of the table.

私のUITableViewControllerのサブクラスの内部コードです:原因セルの再利用へ

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return [sectionNames count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    NSArray *headingsSection = [cellTitles objectAtIndex:section]; 
    return [headingsSection count]; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    return [sectionNames objectAtIndex:section]; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    self.tableView.allowsSelection = YES; 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    cell.textLabel.text = [[cellTitles objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 
    cell.textLabel.textColor = [UIColor blackColor]; 
    cell.textLabel.font = [UIFont boldSystemFontOfSize:15]; 

    switch (indexPath.section) { 
    case 0: 
     cell.detailTextLabel.text = [NSString stringWithFormat:@"%d%%", [[assistSettingsArray_glob objectAtIndex:indexPath.row] intValue]]; 
     break; 
    case 1: 
     switch (indexPath.row) { 
     case 0: 
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
      break; 
     case 1: 
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
      break; 
     case 2: 
      if (defaultAssistOn) { 
       cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      } else { 
       cell.accessoryType = UITableViewCellAccessoryNone; 
      } 
      break; 
     } 
     break; 
    } 

    return cell; 
} 

答えて

8

、あらかじめ設定された値を持つ細胞が再表示されます。 switch (indexPath.section) ...

、デフォルトにdetailTextLabelとaccessoryTypeを初期化:parantheisis内の各スイッチケースでコードを囲む

cell.detailTextLabel.text = @""; 
cell.accessoryType = UITableViewCellAccessoryNone; 
+1

また、サブビューをコンテンツビューに追加する場合は、[[[cell contentView]サブビュー] makeObjectsPerformSelector:@selector(removeFromSuperview)]; – Winder

+0

ああ、それは問題を完全に解決しました。どうもありがとう。 :) – Sabobin

0

試行を。したがって、上記のコードは次のようになります。

switch (indexPath.section) { 
    case 0: { 
     cell.detailTextLabel.text = [NSString stringWithFormat:@"%d%%", 
       [[assistSettingsArray_glob objectAtIndex:indexPath.row] intValue]]; 
     break; 
    } 
    case 1: { 
     switch (indexPath.row) { 
      case 0: { 
       cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
       break; } 
     case 1: { 
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
      break; } 
     case 2: { 
      if (defaultAssistOn) { 
       cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      } 
      else{ 
       cell.accessoryType = UITableViewCellAccessoryNone; 
      } 
      break; } 
     default: 
      break; 
     } 
     break; 
    } 
    default: 
    break; 
    } 

私はこの動作がわかりません。それができるかどうかを知らせてください:)

+0

前の答えは実際に問題を解決しました。ご協力いただきありがとうございます。 – Sabobin