【发布时间】:2014-04-25 04:30:15
【问题描述】:
我正在学习objective-c,并在我的书中找到了以下代码。我有 3 个问题,
- 显然,它通过实现两个必需的方法来符合协议,但为什么不在标头中写入
<UITableViewDataSource,UITableViewDelegate>就可以工作? - 它符合哪个协议,
UITableViewDataSource或UITableViewDelegate? - 为什么没有
UITableView.delegate = self?
这是代码,
@implementation ItemsViewController
-(instancetype) init
{
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
for (int i = 0; i < 5; i++)
{
[[ItemStore sharedStore] creatItem];
}
}
return self;
}
-(instancetype) initWithStyle:(UITableViewStyle)style
{
return [self init];
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[ItemStore sharedStore] allItems] count];
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *c = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
NSArray *items = [[ItemStore sharedStore] allItems];
Item *item = [items objectAtIndex:indexPath.row];
c.textLabel.text = [item description];
return c;
}
-(void) viewDidLoad
{
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
}
@end
感谢您帮助理解这一点。
【问题讨论】:
标签: ios objective-c uitableview delegates datasource