【问题标题】:problems with deleting a cell of a table删除表格单元格的问题
【发布时间】:2011-12-06 08:23:34
【问题描述】:

在我的表 (UITableView) 中,我使用带有 UITextField 的单元格而不是 UILabel,通过“addSubview”添加到单元格中。我需要这个,因为我希望我的单元格可以直接编辑。作为单元格样式,我使用UITableViewCellStyleDefault。 - 一切正常:我可以随时添加和编辑单元格。

但是,删除会产生问题:当我“删除”一个单元格并在表格中创建一个reloadData 时,该单元格仍会显示其旧内容以​​及新内容。它下面的所有单元格也一样。当我关闭我的应用程序并重新启动它时,表格会正确显示。

这里是我用来删除单元格的代码

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
                                            forRowAtIndexPath:(NSIndexPath *)indexPath {

NSUInteger row = [indexPath row];
NSUInteger count = [datas count];

if (row <= count) {

    NSString* data = [datas objectAtIndex: [indexPath row]];
    [self deleteDatas:data];
}

[self.locationTable reloadData];

}

deleteDatas 中,我只是从文件中删除了相应的数据,该文件可以正常工作,正如新加载应用程序所“证明”的那样。

这里

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";    
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    longPressGesture.minimumPressDuration = 2.0;
    [cell addGestureRecognizer:longPressGesture];
}

// Configure the cell.

// table cell with uitextfield instead of lable.
UITextField* textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 185, 30)];
textField.enabled = NO;
[cell.contentView addSubview:textField];

NSUInteger count = [datas count];
NSUInteger row = [indexPath row];

// last cell is empty to edit it
if (row+1 < count) {

    textField.text = [datas objectAtIndex:[indexPath row]];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}

return cell;

}

有什么想法吗? - 谢谢

任何想法,为什么我的单元格显示两倍的内容(一次是原始单元格,一次是下面单元格的内容?) - 我认为s.th。重新加载单元格是错误的。 - 文本字段是否有可能出现问题? - 我怎样才能解决这个问题?

【问题讨论】:

  • 你能把你在你的方法中写的代码贴出来吗 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  • 并检查您的 IBOutlet 连接
  • 在此处粘贴您的 cellFroRowAtIndexPath。
  • 请在上面找到代码。如何检查单元格的 IBOutlet 连接?

标签: iphone xcode uitableview uitextfield


【解决方案1】:

你可能应该像这样编写你的 commitEditingStyle: 方法:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  if (editingStyle == UITableViewCellEditingStyleDelete) {
    if (indexPath.row <= [data count]) {
      // Update the model by deleting the actual data
      NSString* data = [datas objectAtIndex:indexPath.row];
      [self deleteDatas:data];
      // Delete the row from the table
      [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];
    }
  }
}

其他要检查的事情是,您的 tableView:numberOfRowsInSection: 方法是否返回正确的数据,如果没有,那么当表尝试删除行并且逻辑不添加时,您将获得断言失败起来。

【讨论】:

  • 代码导致断言失败,所以我的“numberOfRowsInSection”错了吗?
  • 是的,可能。你能粘贴什么 deleteDatas: 吗?为什么不直接做 [datas removeObjectAtIndex:indexPath.row] ?
【解决方案2】:

我认为您还应该在 commitEditingStyle 中从 mutablearray 中删除该元素...

NSString* data = [datas objectAtIndex: [indexPath row]];
[self deleteDatas:data];
[datas removeObjetAtIndex: [indexPath row]];

否则在下一次 reloadData 时,字符串仍然存在于内存中并显示在 cellForRow...

【讨论】:

  • 已经在deteDatas:data´. There i "create" datas´ new 中完成