2012-02-02 2 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를 업데이트해야합니다. 그렇지 않으면 데이터 소스와 뷰가 동기화되지 않습니다. 당신은

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

을 넣어뿐만 아니라 있도록해야

[tableView insertRowsAtIndexPaths:rows withRowAnimation:UITableViewRowAnimationAutomatic]; 

보기 및 동기화 모델 숙박 :

그래서 당신은 전화를 할 때마다. 물론 위의

은 의사입니다 (당신이 당신의 모델 객체는 어떤의 인스턴스를 생성해야 할 것이다), 및 배열이있는 NSMutableArray해야 할 것이다 또는 당신이 그것을에 삽입 할 때 충돌합니다.

+0

해결 방법이 아닙니다. NSArray "comments"는 모든 셀에 대한 NSDictionaries를 포함하는 NSArrays를 포함합니다. 그러나 nsarray "openedSections"는 그것을 제어합니다. – werbary

관련 문제