【发布时间】:2011-07-23 10:51:32
【问题描述】:
我想在联系人应用程序中添加表格标题(而不是部分标题),例如:
完全一样 - 表格上方图像旁边的标签。
我希望所有视图都是可滚动的,所以我不能将它们放在表格之外。
我该怎么做?
【问题讨论】:
标签: iphone ios uitableview
我想在联系人应用程序中添加表格标题(而不是部分标题),例如:
完全一样 - 表格上方图像旁边的标签。
我希望所有视图都是可滚动的,所以我不能将它们放在表格之外。
我该怎么做?
【问题讨论】:
标签: iphone ios uitableview
在 Swift 中:
override func viewDidLoad() {
super.viewDidLoad()
// We set the table view header.
let cellTableViewHeader = tableView.dequeueReusableCellWithIdentifier(TableViewController.tableViewHeaderCustomCellIdentifier) as! UITableViewCell
cellTableViewHeader.frame = CGRectMake(0, 0, self.tableView.bounds.width, self.heightCache[TableViewController.tableViewHeaderCustomCellIdentifier]!)
self.tableView.tableHeaderView = cellTableViewHeader
// We set the table view footer, just know that it will also remove extra cells from tableview.
let cellTableViewFooter = tableView.dequeueReusableCellWithIdentifier(TableViewController.tableViewFooterCustomCellIdentifier) as! UITableViewCell
cellTableViewFooter.frame = CGRectMake(0, 0, self.tableView.bounds.width, self.heightCache[TableViewController.tableViewFooterCustomCellIdentifier]!)
self.tableView.tableFooterView = cellTableViewFooter
}
【讨论】:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.frame.size.width,30)];
headerView.backgroundColor=[[UIColor redColor]colorWithAlphaComponent:0.5f];
headerView.layer.borderColor=[UIColor blackColor].CGColor;
headerView.layer.borderWidth=1.0f;
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5,100,20)];
headerLabel.textAlignment = NSTextAlignmentRight;
headerLabel.text = @"LeadCode ";
//headerLabel.textColor=[UIColor whiteColor];
headerLabel.backgroundColor = [UIColor clearColor];
[headerView addSubview:headerLabel];
UILabel *headerLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, headerView.frame.size.width-120.0, headerView.frame.size.height)];
headerLabel1.textAlignment = NSTextAlignmentRight;
headerLabel1.text = @"LeadName";
headerLabel.textColor=[UIColor whiteColor];
headerLabel1.backgroundColor = [UIColor clearColor];
[headerView addSubview:headerLabel1];
return headerView;
}
【讨论】:
您也可以在界面构建器中仅创建一个 UIView,然后拖放 ImageView 和 UILabel(使其看起来像您想要的标题),然后使用它。
一旦您的 UIView 看起来也符合您的要求,您可以通过 XIB 以编程方式对其进行初始化并添加到您的 UITableView。换句话说,您不必在 IB 中设计 ENTIRE 表。只是headerView(这样header view也可以在其他表中复用)
例如,我的一个表头有一个自定义 UIView。该视图由名为“CustomHeaderView”的 xib 文件管理,并使用我的 UITableViewController 子类中的以下代码将其加载到表头中:
-(UIView *) customHeaderView {
if (!customHeaderView) {
[[NSBundle mainBundle] loadNibNamed:@"CustomHeaderView" owner:self options:nil];
}
return customHeaderView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Set the CustomerHeaderView as the tables header view
self.tableView.tableHeaderView = self.customHeaderView;
}
【讨论】:
UITableView 有一个 tableHeaderView 属性。将其设置为您想要的任何视图。
使用新的UIView 作为容器,将文本标签和图像视图添加到新的UIView,然后将tableHeaderView 设置为新视图。
例如,在UITableViewController:
-(void)viewDidLoad
{
// ...
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
[headerView addSubview:imageView];
UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
[headerView addSubview:labelView];
self.tableView.tableHeaderView = headerView;
[imageView release];
[labelView release];
[headerView release];
// ...
}
【讨论】:
public override UIView GetViewForHeader(UITableView tableView, nint section) { return headerView; } public override nfloat GetHeightForHeader(UITableView tableView, nint section) { return headerView.Frame.Height; }
您可以在 Interface Builder 中轻松完成。只需创建一个带有表的视图并将另一个视图拖放到表上。这将成为表头视图。将您的标签和图像添加到该视图。有关视图层次结构,请参见下图。
【讨论】: