2013-03-31 39 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