无法使UITableView显示对象中的内容 - Objective C

时间:2013-03-31 05:27:48

标签: objective-c uitableview

我正在学习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 我希望你的家伙可以帮助我:(

1 个答案:

答案 0 :(得分: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,但这应该会让您超越第一个问题。