Dan*_*own 38 iphone uitableview
我有一个使用所有默认UITableView内容创建的视图,但现在我需要在其上方添加标题区域UITableView(因此UITableView将正常滚动,但屏幕的前100px左右将具有静态标头内容).我没有看到我可以UITableView在IB中调整大小,我不知道该怎么做.
有人知道吗?
noo*_*_es 57
您可以使用UITableViewDelegate方法为表创建自定义标题视图并指定高度,即tableView:viewForHeaderInSection:和tableView:heightForHeaderInSection:.您可以向视图添加任何您喜欢的内容.这是一个添加右对齐的示例UILabel:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.frame.size.width,30)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, headerView.frame.size.width-120.0, headerView.frame.size.height)];
headerLabel.textAlignment = NSTextAlignmentRight;
headerLabel.text = [titleArray objectAtIndex:section];
headerLabel.backgroundColor = [UIColor clearColor];
[headerView addSubview:headerLabel];
return headerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 30.0;
}
Run Code Online (Sandbox Code Playgroud)
Osc*_*mez 46
你为什么不使用UITableView提供的标题?如下:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"My Title";
}
Run Code Online (Sandbox Code Playgroud)
此外,您可以通过拖动边框在IB中调整表格视图的大小.
lit*_*ium 16
当您将一个UIView或一个子类添加到UITableView使用IB时(只需拖动UIView并将其拖放到您的UPPER部分UITableView),它会自动添加该UIView组件并使其成为"tableHeader"组件.
每个UITableView都有一个tableHeader和一个tableFooter组件保留...
这样,新视图将成为UITable的一部分,并随之滚动或显示/消失或对表执行的任何操作.如果需要条件行为,可以更改其隐藏属性.
另一方面,如果您希望标题视图保持不变,那么当表格滚动时,最好使表格更小并将标题放在其上方,如其他答案所示...
我终于在不改变基类的情况下以正确的方式解决了这个问题.将视图添加到父导航控制器的一个答案很好,但转换看起来很糟糕.
修复实际上很容易.诀窍是为self.tableView属性创建自定义setter和getter.然后,在loadView中,用新的UIView替换视图并将tableView添加到它.然后你可以在tableView周围添加子视图.以下是它的完成方式:
在标题中:
@interface CustomTableViewController : UITableViewController
{
UITableView *tableView;
}
Run Code Online (Sandbox Code Playgroud)
在.m:
- (UITableView*)tableView
{
return tableView;
}
- (void)setTableView:(UITableView *)newTableView
{
if ( newTableView != tableView )
{
[tableView release];
tableView = [newTableView retain];
}
}
- (void)loadView {
[super loadView];
//save current tableview, then replace view with a regular uiview
self.tableView = (UITableView*)self.view;
self.view = [[UIView alloc] initWithFrame:self.tableView.frame];
[self.view addSubview:self.tableView];
//code below adds some custom stuff above the table
UIView *customHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];
customHeader.backgroundColor = [UIColor redColor];
[self.view addSubview:customHeader];
[customHeader release];
self.tableView.frame = CGRectMake(0, customHeader.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - customHeader.frame.size.height);
}
- (void)viewDidUnload
{
self.tableView = nil;
[super viewDidUnload];
}
Run Code Online (Sandbox Code Playgroud)
请享用!
| 归档时间: |
|
| 查看次数: |
63698 次 |
| 最近记录: |