繁体   English   中英

删除UITableView行-复制单元格

[英]Deleting UITableView rows - duplicating cells

尝试删除UITableView的行时,我遇到一些奇怪的行为。

我的应用程序中有两个tableview,它们在tableView:commitEditingStyle:forRowAtIndexPath :方法上几乎具有相同的代码。 一个运行良好,另一个运行有此问题:让我们假设我的表视图中有X行以上,因此,单元格重用和滚动都已启用。 如果我删除列表顶部的一行,则在单击删除按钮之前,底部屏幕上的所有单元格都会全部复制到屏幕底部。 因此,例如,如果我删除5行,那么我的表视图中将具有相同的单元格。 然后,如果我滚动表格视图,它们将全部恢复正常。 (或者如果我更改viewcontroller,然后返回到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] ,根本不会发生删除问题! 这是发生反转数组的原因。 哦,我要提到的是,当我填充单元格时(例如,在加载视图时),单元格的重用工作正常,没有重复的单元格或类似的东西。 只有在删除行时才会发生这种情况。

检查两个表的cellforRowAtIndexPath: 重用单元格的方式应该有所不同。

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

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

您可以做的另一件事是在删除后尝试重新加载表。 希望这能解决您的问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM