【发布时间】:2011-09-07 10:33:55
【问题描述】:
要问一个简单的问题还有很长的路要走,但鉴于 Objective-C 中的指针对某些人来说是多么令人困惑,也许验证这里发生的事情会帮助其他人,以及我自己。
所以,简而言之,我有一个 UITableView,您可以在其中启用编辑和删除行。我想从表视图和数据源中删除该行。原始数据源位于模型类中,但我有一个指针/属性,其中包含本地表视图控制器中的数据。当我通过编辑从表格视图中删除一行时,它似乎可以工作并删除数据......但我不明白为什么。我认为这是因为我对指针的根本误解。
在我的带有 NSMutableArray 的模型类中:
@property (nonatomic,retain) NSMutableArray *allAlarms;
// ... plist gets read and serialized to an array ...
allAlarms = [NSMutableArray arrayWithArray:[plistValues objectForKey:@"Alarms"]];
在 UITableViewController 类中,我在 viewWillAppear 期间通过属性将可变数组拉入:
@property (nonatomic, retain) NSMutableArray *alarms;
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// retrieve values from data controller
self.alarms = [dataController returnAllAlarms];
[self.tableView reloadData];
}
当执行编辑的委托方法时,我这样做了,它可以工作......
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the local data source
[self.alarms removeObjectAtIndex:indexPath.row];
// The above line appears to delete it in the local pointer as well as in the original property in the model class
// so I just have to tell the data controller to save plist for when it's read back in via viewWillAppear
[dataController writePlist];
// Delete the row from the table view
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
为什么我的 tableView:commitEditingStyle:forRowAtIndexPath 中发生的事情最终会起作用?在我的本地类中从指向 NSMutableArray 的指针中删除一项是否也会在远程类的原始指针中删除它,因为前者是指向后者的指针?
我需要了解,因为我将在此本地指针上执行其他操作,并希望确保我不会因为想要在远程属性上执行它而变得多余,等等。有什么关于我的'我在做不直观和多余的事情,还是在我继续学习 Objective-C 并更多地使用指针时它会更有意义?
【问题讨论】:
标签: ios uitableview pointers nsmutablearray datasource