【问题标题】:checkboxes in uitableview are giving errorsuitableview 中的复选框给出错误
【发布时间】:2013-07-04 17:02:03
【问题描述】:

我有 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];

}

我有两个数组:处理第 0 节中的对象的 taskArray 和处理第 1 节中的对象的 completedArray。

我收到错误 * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'

【问题讨论】:

  • 您使用的数组为空。检查您创建它的方式并填充它。您也可以使用@dorada 解决方案找到合适的单元格。

标签: iphone ios objective-c uitableview


【解决方案1】:

两件事。

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

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

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

【讨论】:

  • 我得到一个错误“不兼容的指针类型初始化 UITableViewCell *__strong 与 UIView 类型的表达式 *
  • 并且要解决第一件事,我应该将手势的初始化放在重用标识符所在的位置吗?
猜你喜欢
  • 2017-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-23
  • 1970-01-01
  • 2012-05-31
相关资源
最近更新 更多