从NSMutableArray中删除for循环中的对象

Jas*_*man 1 xcode objective-c uitableview nsmutablearray ios

我正在使用a UITableView和for数组中的每个对象作为数据源UITableView,如果它们符合某个if语句,我将删除它们.我的问题是它只删除数组中的每个其他对象.

码:

UIImage *isCkDone = [UIImage imageNamed:@"UITableViewCellCheckmarkDone"];
int c = (tasks.count);
for (int i=0;i<c;++i) {
    NSIndexPath *tmpPath = [NSIndexPath indexPathForItem:i inSection:0];
    UITableViewCell * cell = [taskManager cellForRowAtIndexPath:tmpPath];
    if (cell.imageView.image == isCkDone) {
        [tasks removeObjectAtIndex:i];
        [taskManager deleteRowsAtIndexPaths:@[tmpPath]
                withRowAnimation:UITableViewRowAnimationLeft];
    }
}
Run Code Online (Sandbox Code Playgroud)

这有什么问题?

her*_*ube 6

你必须向后运行你的循环,即

for (int i=c-1;i>=0;--i)
Run Code Online (Sandbox Code Playgroud)

如果以相反的方式运行它,则删除索引位置处i的对象会移动阵列中前方i一个位置后面的对象.最后,您甚至可以遍历数组的边界.