【发布时间】: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