我是个新手程序员。因为我是Stackoverflow公司的一名新兵,我还不能评论别人。我有一个关于这篇文章的问题:How to use NSUserDefaults to save data from ViewController and retrieve it to TableViewController when app reruns
我试着解决这个问题,我读了很多关于Stackoverflow的主题,但是我找不到解决方案。我有与Rhenzzz相同的代码,我实现了Jay解释的解决方案。
在cellForRowAtIndexPath方法中,NSString *itemWish = itemDictionary[@"itemWish"];行有一个错误。
错误:
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;
}编辑:
我忘记了,当我添加一个新的项目时,我的错误会发生。当我启动应用程序时,tableview会显示数据。但是,当我在ViewController上添加数据,然后单击Done按钮保存并返回到Tableview时,我会看到上面描述的错误。
所以我把一些NSLog放在cellForRowAtIndexPath中:NSLog(@"Display the dictionary:%@",itemDictionary );和NSLog(@"Display the item:%@",itemWish );。一切都很好。
所以我的问题当然来自于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并不是自动更新的。
谢谢你的帮助!
发布于 2014-05-13 23:32:31
考虑到这个错误,很明显,self.wishlistItem是一个WishListItem对象数组,而不是一个字典数组。您应该更改这两行:
NSDictionary *itemDictionary = self.wishlistItem[indexPath.row];
NSString *itemWish = itemDictionary[@"itemWish"];至:
WishListItem *wishListItem = self.wishlistItem[indexPath.row];
NSString *itemWish = wishListItem.somePropertyGivingTheDesiredValue;显然,您需要在wishListItem上使用适当的属性来获得您想要的值。
https://stackoverflow.com/questions/23642838
复制相似问题