gpt4 book ai didi

iphone - UITableView在离开框架时在单元格上随机滴答

转载 作者:行者123 更新时间:2023-12-01 18:32:10 24 4
gpt4 key购买 nike

我有一个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;
}

我感觉这与它有关,但我真的无法分辨。

这是滚动之前的样子:

当我向上滚动到顶部时,将发生以下情况:

我没有点击那个前一个。如果我上下滚动,可能会被取消勾选,而另一个会被勾选。

可能很重要,所以这里是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]);
}
}

}
}

最佳答案

您没有保存已勾选的项目。您应该将索引保存在数组中,并在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;
}

请记住,由于单元的重用,最后的else块非常重要。

关于iphone - UITableView在离开框架时在单元格上随机滴答,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7749787/

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