2013-03-31 104 views
0

我正在学习Objective C,在这种语言中我是全新的,现在我被UITableView卡住了。 我正在阅读电子书,但他们在书中写的与现在的Xcode版本有些不同。我试图自己修改一天,但仍然没有任何线索。无法使UITableView显示对象中的内容 - Objective C

这里是我的我的书屋对象。

#import "Bookstore.h" 
@implementation Bookstore 
@synthesize myBookStore; 

- (id)init{ 
    self = [super init]; 
    if(self){ 
     self.myBookStore = [[NSMutableArray alloc] init]; 
     Book *newBook = [[Book alloc] init]; 
     newBook.title = @"Objective C for Absolute Beginner"; 
     newBook.author = @"Bennett, Fisher and Lees"; 
     newBook.desc = @"iOS programming made easy"; 
     [self.myBookStore addObject:newBook]; 

     newBook = [[Book alloc] init]; 
     newBook.title = @"A Farewell To Arms"; 
     newBook.author = @"Ernest Hemingway"; 
     newBook.desc = @"The story of an affair between an English " 
     "nurse and an American soldier " 
     "on the Italian front " 
     "during World War I."; 
     [self.myBookStore addObject:newBook]; 
    } 

    return self; 

} 
- (NSUInteger)count{ 
    return myBookStore.count; 
} 
- (Book *)bookAtIndex:(NSInteger)index{ 
    return [myBookStore objectAtIndex:index]; 
} 
@end 

这里是在MasterViewController.m代码,我试图修改 首先我初始化我的对象在我的电子书教程。

#import "LKMasterViewController.h" 
#import "Bookstore.h" 
#import "Book.h" 
#import "LKDetailViewController.h" 

@interface LKMasterViewController() { 
    NSMutableArray *_objects; 
} 
@end 

@implementation LKMasterViewController 

@synthesize myBookStore; 

// INIT MY BOOK OBJECT // 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 

    if (self) { 
     self.title = NSLocalizedString(@"Master", @"Master"); 
     self.myBookStore = [[Bookstore alloc] init]; 
    } 
    return self; 
} 
// END INIT // 
- (void)awakeFromNib 
{ 
    [super awakeFromNib]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.navigationItem.leftBarButtonItem = self.editButtonItem; 

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)]; 
    self.navigationItem.rightBarButtonItem = addButton; 
} 

Then I modify in my UITableView 

#pragma mark - Table View 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return myBookStore.count; // THIS IS A NUMBER OF ROW IN SECTION, IT'S EQUAL TO NUMBER OF OBJECT IN OUR ARRAY 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 

// NSDate *object = _objects[indexPath.row]; // THIS'S DEFAULT BUT I COMMENT IT 
    cell.textLabel.text = [self.myBookStore bookAtIndex:indexPath.row].title; // THIS IS WHERE I MODIFIED 
    return cell; 
} 

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [_objects removeObjectAtIndex:indexPath.row]; 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. 
    } 
} 

请帮助我,我知道这可能是一个愚蠢的问题,但我不能自我解决困难>” <,它似乎我的电子书是过时:( 我忘了提,没有例外,没有问题或错误,当我运行这个程序,唯一的一点是有在主视图中没有任何东西显示了>” <

注:我把我的源代码在这里:http://wikisend.com/download/793354/myBookStore.zip 我的人希望你可以帮助我:(

+0

你想显示存储在书中对象UITableView的内容? –

+0

是你的代表调用,在numberOfRows和cellForRow中加入了一些nslog .. –

+0

除非你的'LKMasterViewController'类继承自'UITableViewController'(看起来不像它,从'initWithNibName:bundle:'方法判断),你已经可能只是忘记了设置表视图的'delegate'和'dataSource'。你的书应该提到如何做到这一点,即使它已经过时。 – omz

回答

0

从看你的项目,这个问题似乎那就是你使用了一个故事板(当你的书被写入时,这可能不存在)。故事板不会使用initWithNibName:bundle:方法实例化视图控制器,而是使用initWithCoder:,因此您的myBookstore属性永远不会初始化(因此您总是可以获得0的书数)。

像这样的东西更换initWithNibName:bundle:方法:

- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
    self = [super initWithCoder:aDecoder]; 
    if (self) { 
     self.myBookStore = [[Bookstore alloc] init]; 
     self.title = NSLocalizedString(@"Master", @"Master"); 
    } 
    return self; 
} 

你会碰到一些问题,因为在你的表视图的方法,你有时会使用myBookstore,有时_objects,但这应该让你在第一次碰撞。

+0

谢谢那么这么多,这是我想知道什么,我知道在我的电子书他们没有故事情节,但我试图从他们的榜样的开始来管理你的帮助是非常有用的,即使我不深刻理解的initWithCoder方法。我很抱歉,因为我不能投票给你,我现在没有足够的声望投票,但是我会回来投票,谢谢。 – lionK