gpt4 book ai didi

iphone - 如何使用 reloadRowsAtIndexPaths 更新行

转载 作者:行者123 更新时间:2023-12-03 20:56:18 26 4
gpt4 key购买 nike

我正在尝试使用按钮删除单元格。

这是单元格的实现。

- (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] autorelease];

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0.f, 0.f, 30.f, 30.f);
[btn addTarget:self
action:@selector(btnTouched:)
forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn];
cell.tag = indexPath.row;
}
return cell;
}

- (void)btnTouched:(UIButton *)sender
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];

[_myMutableArray removeObjectAtIndex:sender.tag];
[_tableView beginUpdates];
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationLeft];
[_tableView endUpdates];
}

这在我删除一行之前效果很好,按钮不会重新加载,因此删除后,我不会删除好的索引。我尝试将 reloadRowsAtIndexPaths:withRowAnimation: for visible indexPath 放在 endUpdates 方法之后,效果非常好。但是删除动画由于 reloadRows 动画而变得丑陋(即使我将其设置为“否”)。那么有没有一种方法可以在不重新加载单元格的情况下重新加载单元格索引路径。或者在不使用标签的情况下删除行。


非常感谢。

最佳答案

您应该能够使用类似于以下的逻辑来查找单元格的索引:

NSIndexPath *indexPath = [_tableView indexPathForCell:(UITableViewCell *)[sender superview]];

使用这种方法,您不需要将行索引的值存储在单元格标记中。

关于iphone - 如何使用 reloadRowsAtIndexPaths 更新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4995166/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com