如何以编程方式设置 UITableView 部分标题(iPhone/iPad)? [英] How to set the UITableView Section title programmatically (iPhone/iPad)?
问题描述
我使用 storyboards
在 Interface Builder 中创建了一个 UITableView
.UITableView
设置了 static cells
和许多不同的部分.
I've created a UITableView
in Interface Builder using storyboards
. The UITableView
is setup with static cells
and a number of different sections.
我遇到的问题是我试图用几种不同的语言设置我的应用程序.为此,我需要能够以某种方式更改 UITableView
部分标题.
The issue I'm having is that I'm trying to setup my app in several different languages. To do this I need to be able to change the UITableView
section titles somehow.
请问有人可以帮我吗?理想情况下,我想使用 IBOutlets
来解决这个问题,但是我怀疑在这种情况下这甚至是不可能的.任何意见和建议将不胜感激.
Please can someone help me out? Ideally I'd like to approach the issue using IBOutlets
however I suspect this isn't even possible in this case. Any advice and suggestions would be really appreciated.
提前致谢.
推荐答案
一旦你将你的 UITableView delegate
和 datasource
连接到你的控制器,你可以做这样的事情:
Once you have connected your UITableView delegate
and datasource
to your controller, you could do something like this:
- (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;
}
迅捷
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
}
这篇关于如何以编程方式设置 UITableView 部分标题(iPhone/iPad)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!