【发布时间】:2014-12-29 12:58:15
【问题描述】:
我有一个简单的项目,它有一个视图,这个视图里面只包含一个表视图。表格视图被分组并且有一个简单的原型单元格。
我已经实现了titleForHeaderInSection 方法来自定义部分标题。当我查看调试输出时,我看到该应用程序正在多次打印 titleForHeaderInSection 中的日志语句。有时两次,有时三次。有谁知道为什么?
这是我的代码:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *initialTitle = @"A title";
NSLog(@"titleForHeaderInSection: %@", initialTitle);
return initialTitle;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *myLabel = [UILabel new];
myLabel.frame = CGRectMake(20, 20, 320, 20);
myLabel.font = [UIFont boldSystemFontOfSize:20];
myLabel.text = @"Test";
UIView *headerView = [UIView new];
[headerView addSubview:myLabel];
return headerView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TestCell" forIndexPath:indexPath];
return cell;
}
@end
这是调试输出:
2014-12-29 14:57:30.806 TestTitleForHeaderInSection[5212:118874] titleForHeaderInSection: A title
2014-12-29 14:57:30.812 TestTitleForHeaderInSection[5212:118874] titleForHeaderInSection: A title
2014-12-29 14:57:30.812 TestTitleForHeaderInSection[5212:118874] titleForHeaderInSection: A title
我还注意到numberOfRowsInSection 方法被多次调用。
【问题讨论】:
-
你设置了节数吗?
标签: ios uitableview