2011-12-28 72 views
0

我有TableView的行數取決於NSMutableArray friendsNames中NSString的數量。不正確的更新UITableView的內容

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return friendsNames.count + 1; 
} 

此外,每一行都顯示NSString在friendsNames的各個索引處。一切似乎都很簡單。但是,當我從friendsNames中刪除一個字符串並使用reloadData方法時,會發生奇怪的事情:UITableView刪除LAST行,而不是剛剛從friendsNames中刪除的字符串。你能解釋我發生了什麼事,我該怎麼辦才能解決這個問題?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.row]; 

MyTableCell *cell = (MyTableCell *)[friendsList dequeueReusableCellWithIdentifier:MyIdentifier]; 

if (cell == nil) { 
    cell = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 

    //create columns 
    for (int i = 0;i < 6;i++) 
     [cell.contentView addSubview:[self createGrid:i :indexPath]]; 
} 
return cell; 
} 

,這裏是它創建columns.it就是BEING從的cellForRowAtIndexPath調用的方法,它是相當難看

- (UILabel *)createGrid:(int)columnIndex :(NSIndexPath *)indexPath 
    { 
CGFloat widths [6] = {35.0,62.0,35.0,35.0,35.0,35.0};//two arrays holding widths of the columns and points where left sides begin 
CGFloat leftSides [6] = {0.0,35.0,97.0,132.0,167.0,202.0}; 

NSArray *titles = [[[NSArray alloc] initWithObjects:@"Status",@"ID",@"Wins",@"Losses",@"Withdrawls",@"Win %", nil] autorelease]; 

UILabel *columnLabel = [[[UILabel alloc] initWithFrame:CGRectMake(leftSides[columnIndex],0.0,widths[columnIndex], friendsList.rowHeight)] autorelease]; 

if (indexPath.row == 0) 
    columnLabel.text = [titles objectAtIndex:columnIndex]; 

else 
{ 
    switch (columnIndex) 
    { 
     case 0: 
     { 
      BOOL isOnline = [[[receivedUsers objectForKey:[friendsNames objectAtIndex:indexPath.row - 1]] objectAtIndex:0] boolValue]; 
      columnLabel.text = isOnline [email protected]"On" :@"Off"; 
     } 
      break; 
     case 1: 
      columnLabel.text = [friendsNames objectAtIndex:indexPath.row - 1]; 
      break; 
     case 2: 
      columnLabel.text = [NSString stringWithFormat:@"%i",[[[receivedUsers objectForKey:[friendsNames objectAtIndex:indexPath.row - 1]] objectAtIndex:1] intValue] ]; 
      break; 
     case 3: 
      columnLabel.text = [NSString stringWithFormat:@"%i",[[[receivedUsers objectForKey:[friendsNames objectAtIndex:indexPath.row - 1]] objectAtIndex:2] intValue] ]; 
      break; 
     case 4: 
      columnLabel.text = [NSString stringWithFormat:@"%i",[[[receivedUsers objectForKey:[friendsNames objectAtIndex:indexPath.row - 1]] objectAtIndex:3] intValue] ]; 
      break; 
     case 5: 
      columnLabel.text = [NSString stringWithFormat:@"%f",[[[receivedUsers objectForKey:[friendsNames objectAtIndex:indexPath.row - 1]] objectAtIndex:4] floatValue] ]; 
      break; 
    } 
} 

columnLabel.layer.borderColor = [[UIColor blackColor] CGColor]; 
columnLabel.layer.borderWidth = 1.0; 
columnLabel.font    = [UIFont systemFontOfSize:8.0]; 
columnLabel.textAlignment  = UITextAlignmentCenter; 
columnLabel.textColor   = [UIColor blackColor]; 
columnLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight; 

return columnLabel; 
} 
+0

如何從friendsNames中刪除字符串?如果你的NSLog friendsName有你正確的數據? – 2011-12-28 17:12:34

+1

可以請您在代碼中刪除對象嗎?我認爲問題可能在於此。 – MadhavanRP 2011-12-28 17:12:59

+0

你有'NSLog'你的陣列,以確保你正在刪除你認爲你是什麼? – 2011-12-28 17:13:18

回答

2

這是一個可重複使用的電池問題。只需更改您的代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *myIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.row]; 

    MyTableCell *cell = (MyTableCell *)[friendsList dequeueReusableCellWithIdentifier:myIdentifier]; 

    if (cell == nil) { 
     //Create a new cell 
     cell = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:myIdentifier] autorelease]; 
    } 

    //Configure the cell 
    //Remove all columns 
    for(UIVIew *subview in cell.contentView.subviews) { 
     [subview removeFromSuperview]; 
    } 
    //Create columns 
    for (int i = 0;i < 6;i++) { 
     [cell.contentView addSubview:[self createGrid:i :indexPath]]; 
    } 
    return cell; 
} 
+0

也想用IB來創建一個包含6個IBOutlets的單元格。使用這種類型的代碼http://stackoverflow.com/questions/540345/how-do-you-load-custom-uitableviewcells-from-xib-files並使用像cell一樣的代碼在configureCell:cell方法中更改您的createGrid方法。 label1.text = [friendsNames objectAtIndex:indexPath.row - 1]; – 2011-12-28 17:43:51