gpt4 book ai didi

iphone - uitableview 中的复选框出现错误

转载 作者:行者123 更新时间:2023-11-29 03:47:54 26 4
gpt4 key购买 nike

我有 2 个部分,我试图将其设置为当您单击其中一个部分中单元格的复选框时,它会转到另一个部分(例如:第 1 部分 -> 第 2 部分)

这是我的一些相关代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
if (!cell)
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCell"];
if([indexPath section] == 0){
cell.textLabel.text = [[[taskArray objectAtIndex:[indexPath row]] taskName] uppercaseString];
cell.imageView.image = [UIImage imageNamed:@"checkboxtry2.png"];
} else if ([indexPath section] == 1) {
cell.textLabel.text = [[[completedArray objectAtIndex:[indexPath row]] taskName] uppercaseString];
cell.imageView.image = [UIImage imageNamed:@"checkboxtry2selected.png"];
}

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handlechecking:)];
[cell.imageView addGestureRecognizer:tap];
cell.imageView.userInteractionEnabled = YES;
return cell;
}


-(void)handlechecking:(UITapGestureRecognizer *)t{
CGPoint tapLocation = [t locationInView:self.tableView];
NSIndexPath *tappedIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation];
NSIndexPath *newIndexPath = nil;
if (tappedIndexPath.section == 0) {
[completedArray addObject:[taskArray objectAtIndex:tappedIndexPath.row]];
[taskArray removeObject:[taskArray objectAtIndex:tappedIndexPath.row]];
newIndexPath = [NSIndexPath indexPathForRow:tappedIndexPath.row inSection:1];
}
else {
[taskArray addObject:[completedArray objectAtIndex:tappedIndexPath.row]];
[completedArray removeObject:[completedArray objectAtIndex:tappedIndexPath.row]];
newIndexPath = [NSIndexPath indexPathForRow:tappedIndexPath.row inSection:0];
}
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:tappedIndexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];

}

我有两个数组:taskArray 处理第 0 部分中的对象,completedArray 处理第 1 部分中的对象。

我收到错误* 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“* -[__NSArrayM objectAtIndex:]:索引 0 超出空数组的范围”

最佳答案

2 件事。

  • 您正在向表格 View 单元格重复添加手势识别器,即使这些单元格已被重复使用并且已经有一个。

此外,在您的手势识别器目标中,您可以执行类似这样的操作,这可能更可靠:

UITableViewCell *cell = [[t.view superview] superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

关于iphone - uitableview 中的复选框出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17475306/

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