【问题标题】:App crashes on swipe delete action sometimes in Xcode (Swift)有时在 Xcode (Swift) 中滑动删除操作时应用程序崩溃
【发布时间】:2015-09-24 16:32:51
【问题描述】:

当我滑动删除操作时,我的待办事项列表应用程序有时会崩溃。

日志显示以下消息:

ibobjc.A.dylib`objc_msgSend + 11,队列 = 'com.apple.main-thread',停止原因 = EXC_BAD_ACCESS(代码=EXC_I386_GPFLT)

我的滑动删除代码

func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
        if (editingStyle == UITableViewCellEditingStyle.Delete) {

            toDoList.removeAtIndex(indexPath.row)
            toDoListTable.reloadData()
            NSUserDefaults.standardUserDefaults().setObject(toDoList, forKey: "toDoList")




        }
    }

任何帮助将不胜感激。

【问题讨论】:

  • 是删除第一项还是最后一项?检查你的界限...
  • 我对xCode不熟悉,但是出现这样的错误,我想知道是否存在权限问题。也许试图访问被另一个线程锁定的一些内存/存储空间?
  • EXC_BAD_ACCESS 是在访问不存在的内存时产生的。您是否正在访问toDoList 中的现有元素?尝试在 toDoList.removeAtIndexpo 要删除的项目之前放置一个断点

标签: ios xcode swift crash


【解决方案1】:

基于Apple Documentation,您应该调用

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation;

以下是 Apple 文档中的示例代码。请注意,它首先从数据源中删除该项目。然后执行deleteRowsAtIndexPaths: withRowAnimation:

试试看

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// If row is deleted, remove it from the list.
if (editingStyle == UITableViewCellEditingStyleDelete) {
    [toDoList removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

【讨论】:

    猜你喜欢
    • 2023-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多