2014-07-23 2 views
0

initWithCoder : 메소드에 문제가 있습니다. 읽는 줄에initWithCoder : 메소드가 화면에 제대로 출력되지 않습니다.

item.text = [NSString stringWithFormat:@"Item for %@", list.name]; 

"항목"은 화면에 출력되지 않습니다. list.name이 정상적으로 인쇄됩니다.

무엇이 문제입니까?

#import "AllListsViewController.h" 
#import "Checklist.h" 
#import "ChecklistViewController.h" 
#import "ChecklistItem.h" 

@interface AllListsViewController() 

@end 

@implementation AllListsViewController 
{ 
    NSMutableArray *_lists; 
} 

- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
    if ((self = [super initWithCoder:aDecoder])) { 
     _lists = [[NSMutableArray alloc] initWithCapacity:20]; 

     Checklist *list; 

     list = [[Checklist alloc] init]; 
     list.name = @"Birthdays"; 
     [_lists addObject:list]; 

     list = [[Checklist alloc] init]; 
     list.name = @"Groceries"; 
     [_lists addObject:list]; 

     list = [[Checklist alloc] init]; 
     list.name = @"Cool Apps"; 
     [_lists addObject:list]; 

     list = [[Checklist alloc] init]; 
     list.name = @"To Do"; 
     [_lists addObject:list]; 

     for(Checklist *list in _lists) { 
      ChecklistItem *item = [[ChecklistItem alloc] init]; 
      item.text = [NSString stringWithFormat:@"Item for %@", list.name]; 
      [list.items addObject:item]; 
     } 

    } 
    return self; 

} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [_lists count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:CellIdentifier]; 
    } 


    Checklist *checklist = _lists[indexPath.row]; 
    cell.textLabel.text = checklist.name; 
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    Checklist *checklist = _lists[indexPath.row]; 

    [self performSegueWithIdentifier:@"ShowChecklist" sender:checklist]; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [_lists removeObjectAtIndex:indexPath.row]; 

    NSArray *indexPaths = @[indexPath]; 
    [tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic]; 
} 


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"ShowChecklist"]) 
    { 
     ChecklistViewController *controller = segue.destinationViewController; 

     controller.checklist = sender; 
    } 
    else if ([segue.identifier isEqualToString:@"AddChecklist"]) { 
     UINavigationController *navigationController = segue.destinationViewController; 

     ListDetailViewController *controller = (ListDetailViewController *)navigationController.topViewController; 
     controller.delegate = self; 
     controller.checklistToEdit = nil; 

    } 
} 

#pragma mark - ListDetailViewControllerDelegate methods 

- (void)listDetailViewControllerDidCancel:(ListDetailViewController *)controller 
{ 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

- (void)listDetailViewController:(ListDetailViewController *)controller didFinishAddingChecklist:(Checklist *)checklist 
{ 
    NSInteger newRowIndex = [_lists count]; 
    [_lists addObject:checklist]; 

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:0]; 

    NSArray *indexPaths = @[indexPath]; 
    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic]; 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

- (void)listDetailViewController:(ListDetailViewController *)controller didFinishEditingChecklist:(Checklist *)checklist 
{ 
    NSInteger index = [_lists indexOfObject:checklist]; 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0]; 
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 
    cell.textLabel.text = checklist.name; 

    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 
{ 
    UINavigationController *navigationController = [self.storyboard instantiateViewControllerWithIdentifier:@"ListNavigationController"]; 

    ListDetailViewController *controller = (ListDetailViewController *)navigationController.topViewController; 
    controller.delegate = self; 
    Checklist *checklist = _lists[indexPath.row]; 
    controller.checklistToEdit = checklist; 
    [self presentViewController:navigationController animated:YES completion:nil]; 
} 


@end 
+0

여기서 item.text를 화면에 출력하려고합니까? – rdelmar

+0

iPhone의 테이블보기로 출력하려고합니다. 지금은 네 개의 셀이 있으며, 각각은 생일, 식료품, 멋진 앱, 할일을 읽습니다. 생일, 식료품 품목 등을 읽으시겠습니까? – pdenlinger

+0

두 가지. 먼저 initWithCoder의 for 루프에서 CheckList 객체 ([list.items addObject : item])에 CheckList 객체 ("item"이라고 함)를 추가합니다. 이게 무슨 뜻이야? 둘째, cellForRowAtIndexPath에 list.items 및 item.text에 액세스하려고 시도하는 곳이 어디에도 표시되지 않으므로 "항목"을 볼 수 없습니다. – rdelmar

답변

0

Item for ... 인 문자열을 셀에 표시하려는 경우 적절한 개체가 표시되지 않습니다. 현재, 내가 볼 수있는 것과, 데이터 구조는 다음과 같습니다 : 당신이 셀의 textLabel 나는 위의 구조에서 // 0에 댓글을 달았습니다 _lists[indexPath.row].name로 설정하는 것으로 보인다

_lists (Array): 
[ 
    list (Checklist): 
    { 
     name (String): @"Birthdays", // 0 
     items (Array): 
     [ 
      item (ChecklistItem): 
      { 
       text (String): @"Item for Birthdays" // 1 
      } 
     ] 
    }, 
    list (Checklist): 
     etc. 

] 

. 당신이 문자열 (// 1에 주석)을 Item for ...에 도착하고자하는 경우 수행해야합니다 :

_lists[indexPath.row].items[0].text 

을 나는 그것이 인덱스 0에있을 것입니다 있으리라 믿고있어, 다른를 추가 할 일이없는 한 데이터를 ChecklistItem에있는 NSMutableArray *items 개체로 가정합니다.

관련 문제