我想将自定义视图添加到每个UITableView部分的标题中。我正在使用这个委托方法来返回所需的视图。它的工作部分是因为它导致单元部分被分散开来,就好像在那里有一个标题一样,然而文本或UIButton实际上都没有出现。我知道这个方法正在被调用,因为我在方法中放置了一个NSLog来查看它是否是。我是否犯了一个愚蠢的错误,或者这是不是这样做的正确方法?在UITableView中设置自定义标题视图
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView* customView = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, tableView.bounds.size.width, 44.0)];
customView.backgroundColor = [UIColor clearColor];
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 44.0)];
headerLabel.textColor = [UIColor darkGrayColor];
headerLabel.font = [UIFont boldSystemFontOfSize:16];
UIButton *headerButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// add button to right corner of section
return customView;
switch (section) {
case 0:
headerLabel.text = @"Team Name";
break;
case 1:
headerLabel.text = @"Captain";
break;
case 2:
headerLabel.text = @"Wicket Keeper";
break;
case 3:
headerLabel.text = @"Batting Order";
headerButton.center = CGPointMake(160.0, 22.0);
headerButton.backgroundColor = [UIColor blueColor];
headerButton.tag = section;
[headerButton addTarget:self action:@selector(enableCellReordering:) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:headerButton];
break;
default:
break;
}
[customView addSubview:headerLabel];
return customView;
}
您是否实现了'tableView:heightForHeaderInSection:'委托方法?如果你实现'tableView:viewForHeaderInSection:',这是必需的。 – rmaddy 2013-04-30 18:08:07
是:'返回44.0' – simonthumper 2013-04-30 18:08:50
为什么你返回'customView'两次? – 2013-04-30 18:10:12