簡體   English   中英

線程1:EXC_BAD_ACCESS(代碼= 1,地址= 0x14)

[英]thread 1:EXC_BAD_ACCESS (code=1, address=0x14)

我正在嘗試創建從tableview中刪除行的功能,這就是我的做法。 錯誤來自於

[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath] withRowAnimation:UITableViewRowAnimationFade];

任何幫助將非常感謝。 謝謝!

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {


    if (editingStyle == UITableViewCellEditingStyleDelete) {

        [self.homeworkArray removeObjectAtIndex:indexPath.row];

        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }

    [self.tableView reloadData];

}

arrayWithObjects:方法需要一個nil來終止列表,所以你需要說 -

[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationFade];
}

甚至更簡單,使用arrayWithObject:

[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

或者正如@rmaddy指出的那樣,使用數組文字語法 -

[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

此外,您不需要reloadData調用 - deleteRowsAtIndexPaths將負責更新表視圖中的相關行。

它可能對你有幫助我知道它與另一個答案相同但它是我自己的答案: -

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle   forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (editingStyle == UITableViewCellEditingStyleDelete)
        {
            [arrStations removeObjectAtIndex:indexPath.row];
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM