我正在学习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