繁体   English   中英

通过UITableView从NSMutableArray删除项目

[英]Delete items from NSMutableArray through UITableView

你好

我想通过UITableView从NSMutableArray删除项目,但应用程序崩溃。 崩溃是“ 0 objc_msgSend ”。

这是我的代码专家:

- (void)viewDidLoad
{
    paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    path = [basePath stringByAppendingPathComponent:@"favoris.plist"];
    dict = [[NSArray arrayWithContentsOfFile:path] mutableCopy];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

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

    [dict removeObjectAtIndex:indexPath.row];

    [self.tableView reloadData];
}

谢谢

EXEC_BAD_ACCESS绝对意味着有一个僵尸,所以我认为您的内存管理过程有些奇怪,这可能是由于实例化数组的方式而发生的。 在Instruments中运行“僵尸”工具绝对可以为您提供更多信息,但是您可以尝试以下操作:

(另一个建议,为什么将NSArray的变量命名为dict?)。

试试这个代码:

- (void)viewDidLoad
{
    paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    path = [[basePath stringByAppendingPathComponent:@"favoris.plist"] retain];
    dict = [[NSMutableArray alloc] initWithContentsOfFile:path];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [dict removeObjectAtIndex:indexPath.row];
    [dict writeToFile:path atomically:YES]; 
    [self.tableView reloadData];
}

该建议的基础是,我怀疑在自动释放的对象上调用copy会产生意外的后果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM