2012-02-02 8 views
-1

皆さん、私は自分のnsarrayから読み込んでいるtableviewを持っていますが、セルを管理した後に、正しくない場所に要素が表示されます。 生成細胞:uitableviewで行を削除して追加するには

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSArray *currentArray = [comments objectAtIndex:indexPath.section]; 
    NSDictionary *currentComment = (NSDictionary *)[currentArray objectAtIndex:indexPath.row]; 

    static NSString *CellIdentifier = @"TitleCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    int commentLevel = [[currentComment objectForKey:@"level"] intValue]; 
    NSString *commentText = [currentComment objectForKey:@"text"]; 
    cell.textLabel.tag = 0xff ; 
    cell.textLabel.numberOfLines = 0; 
    cell.textLabel.font = [UIFont fontWithName:@"Verdana" size:17.0]; 
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; 
    cell.textLabel.text = commentText; 
    cell.textLabel.backgroundColor = [UIColor clearColor]; 
    if (commentLevel > 0 && cell.imageView.image == nil) { 
     cell.imageView.image = [UIImage imageNamed:@"06-arrow-northwest.png"]; 
    } 
    if (commentLevel == 0 && [[openedSections objectAtIndex:indexPath.section] boolValue] == NO) { 
     if ([currentArray count] > 1) { 
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     } 
    } 
    return cell; 
} 

の編集方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    if ([[openedSections objectAtIndex:indexPath.section] boolValue] == NO) { 
     [openedSections replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithInt:1]]; 
     NSMutableArray *rows = [[NSMutableArray alloc] init]; 
     int i = 1; 
     while (i < [[comments objectAtIndex:indexPath.section] count]) { 
      NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:indexPath.section]; 
      [rows addObject:[path copy]]; 
      [path release]; 
      i = i + 1; 
     } 
     [tableView beginUpdates]; 
     [tableView insertRowsAtIndexPaths:rows withRowAnimation:UITableViewRowAnimationAutomatic]; 
     [tableView endUpdates]; 
     [rows release]; 
    } else if (indexPath.row == 0) { 
     [openedSections replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithInt:0]]; 
     NSMutableArray *rows = [[NSMutableArray alloc] init]; 
     int i = 1; 
     while (i < [[comments objectAtIndex:indexPath.section] count]) { 
      NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:indexPath.section]; 
      [rows addObject:[path copy]]; 
      [path release]; 
      i = i + 1; 
     } 
     [tableView beginUpdates]; 
     [tableView deleteRowsAtIndexPaths:rows withRowAnimation:UITableViewRowAnimationAutomatic]; 
     [tableView endUpdates]; 
     [rows release]; 
    } 
} 

はあなたの任意のアイデアをお持ちですか?

答えて

2

テーブル内のセルを変更するたびにNSArrayを更新する必要があります。そうしないと、データソースとビューが同期しなくなります。

ですから、呼び出すたび:同期して、あなたのビューとモデル滞在するよう

[tableView insertRowsAtIndexPaths:rows withRowAnimation:UITableViewRowAnimationAutomatic]; 

をあなたにも

for (NSIndexPath *indexPath in rows) 
{ 
    [self.myDataArray insertObject:myRowModel atIndex:indexPath.row]; 
} 

を置く必要があります。

明らかに上記は擬似コードです(モデルオブジェクトが何であってもインスタンスを作成する必要があります)。配列はNSMutableArrayでなければなりません。また、挿入しようとするとクラッシュします。

+0

解決策ではありません。 NSArrayの "comments"には、すべてのセルのNSDictionariesを含むNSArraysが含まれています。しかし、nsarray "openedSections"はそれを制御します。 – werbary

関連する問題