【发布时间】:2013-04-30 11:34:27
【问题描述】:
我正在尝试在UITableView 中添加一个固定透明标题,类似于附加图像上的标题(LHR-SYD / 372 结果)。这是 xcode/ios 中的“内置”组件还是如何完成的?
【问题讨论】:
标签: iphone ios objective-c xcode uitableview
我正在尝试在UITableView 中添加一个固定透明标题,类似于附加图像上的标题(LHR-SYD / 372 结果)。这是 xcode/ios 中的“内置”组件还是如何完成的?
【问题讨论】:
标签: iphone ios objective-c xcode uitableview
使用这些方法,
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
以上设置视图的方法。
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
上面设置标题的方法。 看到这个,
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *lbl = [[[UILabel alloc] init] autorelease];
lbl.textAlignment = UITextAlignmentLeft;
lbl.text=@"LHR-SYD / 372 Results";
return lbl;
}
通过使用上述方法,您可以向 headerview 添加不同的对象。
(或)
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return @"LHR-SYD / 372 Results";
}
我认为这是你的要求。
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 30;
}
您可以使用此代码设置标题视图的高度
【讨论】:
您可以更改默认标题视图 bg:
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
header.backgroundView.backgroundColor = [header.backgroundView.backgroundColor colorWithAlphaComponent:1];
}
【讨论】:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *transparentView=[[UIView alloc]initWithFrame:CGRectMake(0,0,320,10)];
transparentView.backGroundColor=[UIColor clearColor];
return transparentView;
}
【讨论】:
这实际上是UITableView 的默认标头。您所要做的就是实现titleForHeaderInSection 方法,它就会出现。查看该方法的文档,它很有帮助
【讨论】:
是的,这是内置的。随附的屏幕截图是使用部分的UITableView。
您还可以自定义部分标题的视图。请参阅[tableView:viewForHeaderInSection:](https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614901-tableview)。但是,您看到的是默认视图,因此您只需要实现部分和titleForHeaderInSection。
【讨论】: