1)我使用单视图应用程序在 xcode 中启动了一个新项目。
2)我删除了默认的视图控制器并添加了一个新的 UITableViewController
3)在情节提要中,我拖出一个 UITableViewController 并将其设置为我刚刚创建的那个
4) 设置重用标识符
在我的代码中,我尝试覆盖 init 方法来进行一些设置。为什么我的自定义初始化方法没有被调用?当你使用故事板时,你拖出一个 UITableViewController 并将它设置为一个自定义类,你能不重写 initWithStyle: 方法吗?当我将设置放入 viewDidLoad 时,它就起作用了。
这是视图控制器的代码:
#import "ItemsViewController.h"
#import "BNRItem.h"
#import "BNRItemStore.h"
@implementation ItemsViewController
- (id)init
{
// Call the superclass's designated initializer
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
for (int i = 0; i < 10; i++) {
[[BNRItemStore defaultStore] createItem];
NSLog(@"Test init");
}
}
return self;
}
- (id)initWithStyle:(UITableViewStyle)style
{
NSLog(@"test init style");
return [self init];
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
NSLog(@"test tableview rowsinsection");
return [[[BNRItemStore defaultStore] allItems] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"test tableview cellforrow");
// Create an instance of UITableViewCell, with default appearance
// Check for a reusable cell first, use that if it exists
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:@"itemsCell"];
// If there is no reusable cell of this type, create a new one
if (!cell) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"itemsCell"];
}
// Set the text on the cell with the description of the item
// that is at the nth index of items, where n = row this cell
// will appear in on the tableview
[[cell textLabel] setText:@"Hello"];
return cell;
}
@end