Objective c 无法使UITableView显示对象-目标C中的内容
我正在学习Objective C,我对这门语言是全新的,现在我被一个UITableView困住了。 我正在读一本电子书,但他们在书中写的内容与当前的Xcode版本没有什么不同。我已经试着自己修改了一天,但仍然没有任何线索 这是我在书店买的东西Objective c 无法使UITableView显示对象-目标C中的内容,objective-c,uitableview,Objective C,Uitableview,我正在学习Objective C,我对这门语言是全新的,现在我被一个UITableView困住了。 我正在读一本电子书,但他们在书中写的内容与当前的Xcode版本没有什么不同。我已经试着自己修改了一天,但仍然没有任何线索 这是我在书店买的东西 #import "Bookstore.h" @implementation Bookstore @synthesize myBookStore; - (id)init{ self = [super init]; if(self){
#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.
}
}
请帮帮我,我知道这可能是个愚蠢的问题,但我自己解决不了>“从你的项目来看,问题似乎是你在使用故事板(在你写这本书的时候可能不存在)。故事板不会使用
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
,有时使用\u对象
,但这应该可以帮助您克服第一个障碍。是否要在UITableView中显示book对象中存储的内容?是否调用您的委托,在numberOfRows和cellForRow中放置一些nslog。除非您的LKMasterViewController
类继承自UITableViewController
(从initWithNibName:bundle:
方法判断,它看起来不像),否则您可能只是忘记设置表视图的委托和数据源。你的书应该提到如何在某个地方这样做,即使它已经过时了。正如@omz所说,你需要确保UITableView
将你的类作为委托
和数据源
。另外,在您的表视图:cellforrowatinexpath:
方法中,您正在使用dequeueReusableCellWithIdentifier:forindexath:
对单元格进行出列,您是否使用registernb:forcellreuseidentier:
或registerClass:forcellreuseidentier:
注册了具有此标识符的Nib或类?@omz:是的,我想我没有声明一些东西,例如initWithName方法,在我的电子书中,他们没有提到这方面的任何内容,我读了一天又一天>“非常感谢你,这就是我想知道的,我知道在我的电子书中他们没有故事板,但我从他们的例子开始就尝试管理。你的帮助真的很有用,即使我不太了解initWithCoder方法。我现在就去试试。我很抱歉,因为我不能投票给你,我现在没有足够的声誉去投票,但我会回来投票。谢谢。:)