gpt4 book ai didi

ios - 删除 UITableView 行 - 复制单元格

转载 作者:行者123 更新时间:2023-12-01 16:44:09 26 4
gpt4 key购买 nike

在尝试删除 UITableView 中的一行时,我遇到了一些奇怪的行为.

我的应用程序中有两个表格 View ,它们在 tableView:commitEditingStyle:forRowAtIndexPath 上的代码几乎相同: 方法。一个工作正常,另一个有这个问题:假设我的表格 View 上有超过 X 行,我不知道,所以单元重用和滚动都启用了。如果我删除列表顶部的一行,则显示在屏幕底部的单元格在点击删除按钮之前都被底部单元格复制。因此,例如,如果我删除 5 行,我的 tableview 上将有相同的单元格。然后,如果我滚动表格 View ,它们都会恢复正常。 (或者如果我更改 View Controller ,然后返回到 tableview 的那个)

这是工作 TableView 的代码:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
if (search.text.length == 0) {
[tableArray removeObjectAtIndex:indexPath.row];
[goalTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self setBG];
tempArray = [NSArray arrayWithArray:tableArray];
[[NSUserDefaults standardUserDefaults]setObject:tempArray forKey:@"currentArray"];
totalGoals = [[NSUserDefaults standardUserDefaults] integerForKey:@"totalGoals"];
totalGoals = totalGoals - 1;
[[NSUserDefaults standardUserDefaults] setInteger:totalGoals forKey:@"totalGoals"];
[[NSUserDefaults standardUserDefaults] synchronize];
} } }

这是有缺陷的tableview的代码:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
if (search.text.length == 0) {
[tableArray removeObjectAtIndex:indexPath.row];
[goalTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self setBG];
tempArray = [NSArray arrayWithArray:tableArray];
[[NSUserDefaults standardUserDefaults]setObject:tempArray forKey:@"completedArray"];
completedGoals = [[NSUserDefaults standardUserDefaults] integerForKey:@"completedGoals"];
completedGoals = completedGoals - 1;
[[NSUserDefaults standardUserDefaults] setInteger:completedGoals forKey:@"completedGoals"];
[[NSUserDefaults standardUserDefaults] synchronize];
} } }

嗯,它几乎是一样的,所以我在这里有点迷路 - 他们有相同的代码,一个工作正常,另一个不是。这可能是 cellForRowAtIndexPath: 的问题吗?方法?我想知道,但在这种方法上找不到任何东西,而且它们实际上也相同。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * identifier = @"Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil) {
//here I'm setting up the cells, labels, and stuff.
}

if (search.text.length == 0 && [filteredArray count] == 0) {
NSDictionary * dict = [tableArray objectAtIndex:[tableArray count] - 1 - indexPath.row];
//here I use the values on the dict to do some stuff with labels, etc.
}
return cell;
}

问题是,我正在用这条线“翻转”数组:
NSDictionary * dict = [tableArray objectAtIndex:[tableArray count] - 1 - indexPath.row];

如果我不这样做,即当我做类似 [tableArray objectAtIndex:indexPath.row] 的事情时只是,删除问题根本不会发生!这是由于反转数组而发生的事情。
哦,顺便提一下,当我填充单元格时(例如,当 View 正在加载时),单元格重用工作得很好,没有重复的单元格或类似的东西。只有在删除行时才会发生这种情况。

最佳答案

检查您的 cellforRowAtIndexPath:两张 table 。
您重复使用细胞的方式应​​该有所不同。

在 iOS6 及更高版本中,您无需检查 if(cell == nil)正如我在下面所做的

static NSString *CellIdentifier = @"Cell";    
CustomCell *cell = (CustomCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

您可以做的其他事情是在删除后尝试重新加载表。希望这能解决你的问题

关于ios - 删除 UITableView 行 - 复制单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21569198/

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