gpt4 book ai didi

iphone - 移动行后更新 UITableViewCell 标签属性

转载 作者:行者123 更新时间:2023-11-29 13:42:25 24 4
gpt4 key购买 nike

在我的应用程序中,当我在 textFieldDidEndEditing: 中提交编辑更改时,我需要能够跟踪识别 UITextField 来自哪个单元格。为此,我只是在 tableview:cellForRowAtIndexPath: 中的单元格文本字段上设置标签属性。

移动单元格时,会造成一些麻烦。例如,如果我将底部单元格移到顶部,删除现在位于底部的单元格,然后尝试编辑我刚放在顶部的单元格,我的应用程序将崩溃,因为标签属性仍为 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] 返回移动行之前存在的单元格。

最佳答案

在您的 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 方法中,您可以遍历所有 UITextField 您可能已将其作为 subview 添加到表中,然后检查并将新标记设置为-

for (UIView* subview in [[self tableView] subviews]) {
if ([subview isKindOfClass:[UITextField class]]) {
if(subview.tag==oldTag){
subview.tag = newTag;
break;
}
}
}

编辑:再三考虑,您可以重新加载表格数据,以防重置所有剩余单元格的标签。

关于iphone - 移动行后更新 UITableViewCell 标签属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8708229/

24 4 0