首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何以编程方式设置UITableView分区标题(iPhone/iPad)?

如何以编程方式设置UITableView分区标题(iPhone/iPad)?
EN

Stack Overflow用户
提问于 2012-05-09 04:07:12
回答 6查看 137.5K关注 0票数 111

我已经使用storyboards在界面生成器中创建了一个UITableViewUITableView是用static cells和许多不同的部分设置的。

我遇到的问题是,我正在尝试用几种不同的语言设置我的应用程序。为此,我需要能够以某种方式更改UITableView部分的标题。

有没有人能帮帮我?理想情况下,我希望使用IBOutlets来解决这个问题,但是我怀疑在这种情况下这是不可能的。任何建议和建议都将不胜感激。

提前谢谢。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2012-05-09 04:27:54

将UITableView delegatedatasource连接到控制器后,可以执行以下操作:

ObjC

代码语言:javascript
复制
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    NSString *sectionName;
    switch (section) {
        case 0:
            sectionName = NSLocalizedString(@"mySectionName", @"mySectionName");
            break;
        case 1:
            sectionName = NSLocalizedString(@"myOtherSectionName", @"myOtherSectionName");
            break;
        // ...
        default:
            sectionName = @"";
            break;
    }    
    return sectionName;
}

斯威夫特

代码语言:javascript
复制
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    let sectionName: String
    switch section {
        case 0:
            sectionName = NSLocalizedString("mySectionName", comment: "mySectionName")
        case 1:
            sectionName = NSLocalizedString("myOtherSectionName", comment: "myOtherSectionName")
        // ...
        default:
            sectionName = ""
    }
    return sectionName
}
票数 286
EN

Stack Overflow用户

发布于 2015-12-02 18:58:56

如果你正在用Swift编写代码,它看起来就像这样的例子

代码语言:javascript
复制
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
    switch section
    {
        case 0:
            return "Apple Devices"
        case 1:
            return "Samsung Devices"
        default:
            return "Other Devices"
    }
}
票数 16
EN

Stack Overflow用户

发布于 2012-05-09 04:12:26

使用UITableViewDataSource方法

代码语言:javascript
复制
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10505708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档