2014-05-13 6 views
1

저는 초보 프로그래머입니다. 나는 Stackoverflow에서 newby이기 때문에 아직 누군가 anwser에 대해서는 언급 할 수 없다. 이 게시물에 대한 질문이 있습니다 : How to use NSUserDefaults to save data from ViewController and retrieve it to TableViewController when app rerunsNSUserDefaults UITableView

나는이 문제를 해결하려고 노력하고 많은 주제를 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; 
} 

편집 :

나는 새 항목을 추가 할 때 내 오류가 발생하는 정확한 잊지. Appview를 실행하면 tableview에 데이터가 표시됩니다. 그러나 내 ViewController에 데이터를 추가 한 다음 &을 완료하면 내 완료 단추를 클릭하여 Tableview로 돌아갑니다. 위에서 설명한 오류가 있습니다.

그래서 NSLog를 cellForRowAtIndexPath : NSLog(@"Display the dictionary:%@",itemDictionary);NSLog(@"Display the item:%@",itemWish);에 넣었습니다. 모든 것은 맞다.

그래서 내 문제는 IBAction를에서 확실히 온다 : 나는 [self.tableView reloadData];을 삭제하면 오류가 사라집니다하지만 분명히 내있는 tableview이 자동으로 업데이트되지 않습니다

- (IBAction)unwindToList:(UIStoryboardSegue *)unwindSegue { 
    AddItemViewController *source = [unwindSegue sourceViewController]; 
    WishlistItem *item = source.wishItem; 
    if (item != nil) { 
     [self.wishlistItem addObject:item]; 
     [self.tableView reloadData]; 
    } 
} 

.

도움 주셔서 감사합니다.

+1

'self.wishlistItem'과 NSDictionaries의 배열입니까? – random

+1

SO가 훌륭한 자원 인 반면에 프로그래밍에 관한 책을 읽고 공부하는 대신 "C"와 Objective-C 언어 (종이 대신 웹에있는 참고 자료 일 수 있음)를 대체 할 수 없습니다. – zaph

답변

0

오류가 발생하면 self.wishlistItemWishListItem 개의 배열이고 사전의 배열이 아님이 분명합니다. 이러한 두 줄을 변경해야합니다 :

NSDictionary *itemDictionary = self.wishlistItem[indexPath.row]; 
NSString *itemWish = itemDictionary[@"itemWish"]; 

에 : 당신은 당신이 그것에서 원하는 값을 얻을 수 wishListItem에서 해당 속성을 사용할 필요가 분명히

WishListItem *wishListItem = self.wishlistItem[indexPath.row]; 
NSString *itemWish = wishListItem.somePropertyGivingTheDesiredValue; 

.