修复了UITableView中的透明标题

时间:2013-04-30 11:34:27

标签: iphone ios objective-c xcode uitableview

我正在尝试在UITableView中添加一个类似于附加图像上的固定透明标题(LHR-SYD / 372结果)。这是xcode / ios中的“内置”组件还是如何完成的?

an image of an ios app that has a fixed transparent header on top of a uitableview

5 个答案:

答案 0 :(得分:7)

使用这些方法,

- (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;
}

通过使用上述方法,您可以在标题视图中添加不同的对象。

(OR)

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"LHR-SYD / 372 Results";
}

这一个你的要求我是这么认为的。

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

您可以使用此代码设置标题视图的高度

答案 1 :(得分:2)

您可以更改默认标题视图bg:

 - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    header.backgroundView.backgroundColor = [header.backgroundView.backgroundColor colorWithAlphaComponent:1];
}

答案 2 :(得分:1)

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

  UIView *transparentView=[[UIView alloc]initWithFrame:CGRectMake(0,0,320,10)];
  transparentView.backGroundColor=[UIColor clearColor];
  return transparentView;

}

答案 3 :(得分:1)

这实际上是UITableView的默认标头。您所要做的就是实现titleForHeaderInSection方法,它就会出现。查看该方法的文档,它有很多帮助

答案 4 :(得分:0)

是的,它是内置的。附带的屏幕截图是使用部分的UITableView

您还可以自定义部分标题的视图。请参阅[tableView:viewForHeaderInSection:]https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614901-tableview)。但是,您看到的是默认视图,因此您只需实现部分和titleForHeaderInSection