我正在学习Objective C,我对这门语言完全陌生,现在我被UITableView困住了。我正在阅读一本电子书,但他们在书中写的内容与当前的 Xcode 版本略有不同。我已经尝试自己修改了一天,但仍然没有任何线索。
这是我的书店的对象。
#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 希望你们能帮助我:(