【发布时间】:2012-01-03 05:17:02
【问题描述】:
在我的应用程序中,当我在textFieldDidEndEditing: 中提交编辑更改时,我需要能够跟踪识别 UITextField 来自哪个单元格。为此,我只需在 tableview:cellForRowAtIndexPath: 中的单元格文本字段上设置 tag 属性。
当单元格被移动时,这会带来一些麻烦。例如,如果我将底部单元格向上移动到顶部,删除现在位于底部的单元格,然后尝试编辑我刚刚放置在顶部的单元格,我的应用程序将崩溃,因为标签属性仍为 8 (或其他数字),但我的数据源中没有这么多元素。
我试图通过更新tableView:moveRowAtIndexPath:toIndexPath:中的标签来解决这个问题
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
NSString *siteToMove = [[sites objectAtIndex:[fromIndexPath row]] retain];
[sites removeObjectAtIndex:[fromIndexPath row]];
[sites insertObject:siteToMove atIndex:[toIndexPath row]];
if ([toIndexPath section] == 1) {
[[(BookmarkTextEntryTableViewCell *)[[self tableView] cellForRowAtIndexPath:toIndexPath] textField] setTag:[toIndexPath row]];
}
}
但是,这不起作用,因为(BookmarkTextEntryTableViewCell *)[[self tableView] cellForRowAtIndexPath:toIndexPath] 返回移动行之前 存在的单元格。
【问题讨论】:
标签: iphone ios uitableview tags