我正在學習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 我的人希望你可以幫助我:(
你想顯示存儲在書中對象UITableView的內容? –
是你的代表調用,在numberOfRows和cellForRow中加入了一些nslog .. –
除非你的'LKMasterViewController'類繼承自'UITableViewController'(看起來不像它,從'initWithNibName:bundle:'方法判斷),你已經可能只是忘記了設置表視圖的'delegate'和'dataSource'。你的書應該提到如何做到這一點,即使它已經過時。 – omz