修复了UITableview的标题?

DOO*_*iac 17 uitableview ios

我有一个UITableView我想坚持44px子视图.我试过了tableViewHeader,但滚动了表的其余部分.

我尝试搜索,并发现很多人说我需要添加一个UIView超级视图,然后添加我的标题和UITableView.但是我找不到一个关于如何做到这一点的例子.我尝试创建一个新的UIView子类并在IB中布置子视图,但是我遇到了让表控制器与UITable链接的问题(因为我对IB知之甚少).

我怎么能用XIB做到这一点?有人能提供一个例子吗?

感谢您提供的任何帮助.

DOO*_*iac 34

我终于在发布后想出了这个.数据.:)

这就是我所做的,以防其他人遇到同样的问题:

  1. 删除现有UITableViewController及其XIB.他们是垃圾.你真的很生气.

  2. UIViewController使用XIB创建一个新的子类

  3. 在IB打开XIB和你的头的东西和添加UITableViewUIView

  4. 在IB Outlets中,UITableView确保将Delegate和DataSource连接到文件所有者

  5. 在视图控制器的标头中,请务必添加 <UITableViewDelegate, UITableViewDataSource>以实现这些协议

  6. 实现UITableView您熟悉和喜爱的所有常规委托和数据源方法,但UIViewController不是以您习惯的方式实现它UITableViewController

在这之后应该工作.

  • 但是,如果由于其他原因需要将其作为UITableViewController,则无法解决问题. (11认同)
  • 使用vanilla UIViewController的问题是你失去了使用UIRefreshControl的能力,UIRefreshControl仅支持UITableViewController. (3认同)
  • 这应该是公认的答案.覆盖现有方法(如当前接受的答案)应该是最后的手段. (2认同)

Ste*_*ter 9

问题是,UITableViewController的view属性与tableView属性相同.我有同样的问题,想在表格上方放置一些固定的内容.我不想更改基类,因为它提供了许多我不想丢失或破坏的强大功能.

修复实际上很容易.诀窍是为self.tableView属性创建自定义集和get.然后,在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;
    UIView *replacementView = [[UIView alloc] initWithFrame:self.tableView.frame];
    self.view = replacementView;
    [replacementView release];
    [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);
}
Run Code Online (Sandbox Code Playgroud)

请享用!