【发布时间】:2014-05-13 22:44:44
【问题描述】:
我是一个新手程序员。因为我是 Stackoverflow 的新手,所以我还不能评论某人的答案。我有一个关于这篇文章的问题:How to use NSUserDefaults to save data from ViewController and retrieve it to TableViewController when app reruns
我试图解决这个问题,我在 Stackoverflow 上阅读了很多主题,但我找不到解决方案。我的代码与Rhenzzz 相同,我实现了Jay 解释的解决方案。
我在NSString *itemWish = itemDictionary[@"itemWish"]; 行的cellForRowAtIndexPath 方法中有一个错误。
错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[WishListItem objectForKeyedSubscript:]: unrecognized selector sent to instance 0x9dc4d80'
我的完整方法是:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"wishlistCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *itemDictionary = self.wishlistItem[indexPath.row];
NSString *itemWish = itemDictionary[@"itemWish"];
cell.textLabel.text = itemWish;
return cell;
}
编辑:
我忘记准确说明我在添加新项目时发生的错误。当我启动应用程序时,表格视图会显示数据。但是,当我在 ViewController 上添加数据,然后通过单击完成按钮保存并返回 Tableview 时,我遇到了上述错误。
所以我在cellForRowAtIndexPath:NSLog(@"Display the dictionary:%@",itemDictionary ); 和NSLog(@"Display the item:%@",itemWish ); 中加入了一些 NSLog。一切都是对的。
所以我的问题肯定来自 IBAction:
- (IBAction)unwindToList:(UIStoryboardSegue *)unwindSegue {
AddItemViewController *source = [unwindSegue sourceViewController];
WishlistItem *item = source.wishItem;
if (item != nil) {
[self.wishlistItem addObject:item];
[self.tableView reloadData];
}
}
如果我删除 [self.tableView reloadData];,错误就会消失,但显然我的 Tableview 没有自动更新。
感谢您的帮助!
【问题讨论】:
-
是
self.wishlistItem和NSDictionaries的数组吗? -
虽然 SO 是一个很好的资源,但它并不能替代阅读和学习有关编程以及“C”和 Objective-C 语言的书籍(可能是网络上的参考资料而不是纸上的参考资料)。
标签: ios objective-c uitableview