如何覆盖tableView:titleForHeaderInSection:调整静态UITableViews的节头? [英] How to override tableView:titleForHeaderInSection: to adjust section headers of static UITableViews?

查看:331
本文介绍了如何覆盖tableView:titleForHeaderInSection:调整静态UITableViews的节头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩故事板,对于一个控制器,我设置了一个 UITableView ,它由两部分组成。在这两个部分中,我添加了几个静态单元格。
但是,根据代码路径,我想为我的两个部分显示不同的标题
由于没有来源且没有涉及委托,我应该如何覆盖 tableView:titleForHeaderInSection:

I'm playing with storyboards and for one controller I set up a UITableView that consists of two sections. In both sections I have added a couple of static cells. However, depending on code paths, I would like to show different headers for my two sections. As there is no source and no delegate involved, how am I supposed to override tableView:titleForHeaderInSection: ?

推荐答案

使用静态表视图,您可以(并且必须)仍然将数据源连接到UITableViewController。关键是如果实现数据源方法,那么这将覆盖您在表视图中设置的静态内容。你可以毫无问题地覆盖titleForHeader,因为这是你想要做的。

With static table views you can (and must) still connect the datasource to a UITableViewController. The key is that if you implement the datasource methods then this will override the static content you have set up in the table view. You can override titleForHeader without any problems since this is what you want to do.

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0)
        return @"HELLO!";
    else {
        return [super tableView:tableView titleForHeaderInSection:section];
    }
}

将静态表中第0部分的标题设置为你好!,覆盖xib中的标题集。其他人留在xib中。

Sets the title of section 0 in the static table to HELLO!, overriding the title set in the xib. The others are left as the were in the xib.

关键点是静态表的填充方式与动态表完全相同,只是UITableViewController实现了自己的所有数据源方法版本。这些方法可能是从xib文件中读取信息并将适当的信息发送回表视图。如果您想要静态内容,请不要实现或调用super。如果您需要自己的内容,请使用与上述类似的代码。

The key point is that static tables are populated exactly the same way as dynamic tables, except that UITableViewController implements its own versions of all the datasource methods. These methods presumably read the information from the xib file and send back the appropriate information to the table view. If you want the static content, don't implement or call super. If you want your own content, use code similar to that above.

这篇关于如何覆盖tableView:titleForHeaderInSection:调整静态UITableViews的节头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆