【发布时间】:2016-08-18 05:39:00
【问题描述】:
我正在使用一个包含四个部分的表格视图。对于每个部分,我都实现了一个标题视图。当我将tableview style 用作Plain 时,它可以正常工作。但是如果我使用tableview style 作为Grouped 它看起来是有线的。
这就是我实现tableview 委托方法的方式。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
{
return 1;
}
else if(section == 1)
{
return 1;
}
else if (section == 2)
{
return 3;
}
else
{
return 1;
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, 50)];
UILabel *titlelabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, screenSize.width-80, 50)];
if (section == 0)
{
titlelabel.text = @"Flight Summary";
[headerView addSubview:titlelabel];
return headerView;
}
else if(section == 1)
{
titlelabel.text = @"Price Summary";
[headerView addSubview:titlelabel];
return headerView;
}
else if (section == 2)
{
titlelabel.text = @"Traveller Details";
[headerView addSubview:titlelabel];
return headerView;
}
else
{
titlelabel.text = @"Book Now";
[headerView addSubview:titlelabel];
return headerView;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"checkcell"];
return cell;
}
这是我使用tableview样式为Plain时的截图
这是我使用tableview样式为Grouped时的截图
我不知道发生了什么,希望您能帮忙。
【问题讨论】:
-
为什么这只发生在
Groupedtableview style -
你实现
tableView:heightForHeaderInSection:方法了吗? -
不,我没有实现,因为大多数时候我使用
Plain样式并且不需要该方法。 @rmaddy -
阅读
tableView:viewForHeaderInSection:的文档。它明确指出您必须实现tableView:heightForHeaderInSection:。不管是普通表还是分组表。 -
k 明白了,谢谢@rmaddy
标签: ios objective-c uitableview