0

私には2つのセクションがあり、セクションの1つでセルのチェックボックスをクリックすると、他のセクションに移動するようにしようとしています(例:セクション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 の 2 つの配列があります。

エラーが発生しています* キャッチされていない例外 'NSRangeException' が原因でアプリを終了しています。理由: '* -[__NSArrayM objectAtIndex:]: 空の配列の境界を超えたインデックス 0'

4

1 に答える 1