UITableView在离开框架时随机勾选单元格

时间:2011-10-13 05:20:59

标签: iphone objective-c ios xcode uitableview

我有一个UITableView,你只能勾选4个单元格。如果我勾选一定数量的单元格然后向上或向下滚动,它将勾选随机单元格。我不明白这背后的逻辑,因为我用来勾选的唯一方法是DidSelectRowAtIndex,它没有被执行(我在那里放了一个断点来检查它)。我真的不知道是什么导致这个,但这里是cellForRowAtIndex:

- (UITableViewCell *) tableView: (UITableView *) tableView
          cellForRowAtIndexPath: (NSIndexPath *) indexPath
{

        static NSString *cellName = @"PrefsTableViewCell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                                 cellName];
        if (cell == nil)
        {
                cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName] autorelease];
        }

        NSUInteger row = [indexPath row];
        Drink *drink = [drinks objectAtIndex:row];

        cell.textLabel.text = drink.name;
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", drink.caffeine];
        [[cell imageView] setImage:drink.image];

        return cell;
}

我感觉它与此有关,但我无法说出来。

这是滚动之前的样子:

enter image description here

当我向上滚动到顶部时会发生这种情况:

enter image description here

我没有点击那个顶级的。如果我向下再向上滚动它可能会被取消,另一个会被勾选。

这可能很重要,所以这里是didSelectRowAtIndexPath方法:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        UITableViewCell *cell = [tableView cellForRowAtIndexPath: indexPath];

        NSLog(@"TESTING");

        if ((cell.accessoryType == UITableViewCellAccessoryNone) && [[DataContainerSingleton theDataContainerSingleton].favourites count] < 4)
        {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;

                [[DataContainerSingleton theDataContainerSingleton].favourites addObject:[drinks objectAtIndex:indexPath.row]];
                NSLog(@"CHECKED %d", [[DataContainerSingleton theDataContainerSingleton].favourites count]);
        }
        else if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
        {
            for (int i = 0; i < [[DataContainerSingleton theDataContainerSingleton].favourites count]; i++)
            {
                NSString *drinkName = [[drinks objectAtIndex:indexPath.row] name];
                NSString *favName = [[[DataContainerSingleton theDataContainerSingleton].favourites objectAtIndex:i] name];

                if ([drinkName isEqualToString: favName])
                {
                    cell.accessoryType = UITableViewCellAccessoryNone;
                    [[DataContainerSingleton theDataContainerSingleton].favourites removeObjectAtIndex:i];
                    NSLog(@"UNCHECKED %d", [[DataContainerSingleton theDataContainerSingleton].favourites count]);
                }
            }

        }
}

1 个答案:

答案 0 :(得分:4)

您没有保存已勾选的项目。您应该将索引保存在数组中,并在cellForRowAtIndexPath:中设置勾选。像 -

这样的东西
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

        if(![self.tickedIndexPaths containsObject:indexPath]) //tickedIndexPaths is an array
            [self.tickedIndexPaths addObject:indexPath];
        else
            [self.tickedIndexPaths removeObject:indexPath];
}

然后 -

- (UITableViewCell *) tableView: (UITableView *) tableView
          cellForRowAtIndexPath: (NSIndexPath *) indexPath
{

        static NSString *cellName = @"PrefsTableViewCell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                                 cellName];
        if (cell == nil)
        {
                cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName] autorelease];
        }

        NSUInteger row = [indexPath row];
        Drink *drink = [drinks objectAtIndex:row];

        cell.textLabel.text = drink.name;
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", drink.caffeine];
        [[cell imageView] setImage:drink.image];

        if([self.tickedIndexPaths containsObject:indexPath])
        {
              cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
              cell.accessoryType = UITableViewCellAccessoryNone;
        }

        return cell;
}

请记住,由于重复使用单元格,最后一个其他块非常重要。