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

我已经使用storyboards在Interface Builder中创build了一个UITableViewUITableView设置了static cells和许多不同的部分。

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

请有人可以帮我吗? 理想情况下,我想用IBOutlets来解决这个问题,但是我怀疑在这种情况下这是不可能的。 任何意见和build议将非常感激。

提前致谢。

一旦你连接你的UITableView delegatedatasource到你的控制器,你可以做这样的事情:

 - (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; } 

使用UITableViewDataSource方法

 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 

如果你在Swift中编写代码,它会看起来像这样的一个例子

 func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { switch section { case 0: return "Apple Devices" case 1: return "Samsung Devices" default: return "Other Devices" } } 

titleForHeaderInSectionUITableView的一个委托方法,所以如下应用节写的标题文本,

 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return @"Hello World"; } 

注意-(NSString *)tableView: titleForHeaderInSection:不被UITableView调用if - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section在UITableView的delegate中实现;

 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 45.0f; //set height according to row or section , whatever you want to do! } 

部分标签文本被设置。

 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *sectionHeaderView; sectionHeaderView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, tableView.frame.size.width, 120.0)]; sectionHeaderView.backgroundColor = kColor(61, 201, 247); UILabel *headerLabel = [[UILabel alloc] initWithFrame: CGRectMake(sectionHeaderView.frame.origin.x,sectionHeaderView.frame.origin.y - 44, sectionHeaderView.frame.size.width, sectionHeaderView.frame.size.height)]; headerLabel.backgroundColor = [UIColor clearColor]; [headerLabel setTextColor:kColor(255, 255, 255)]; headerLabel.textAlignment = NSTextAlignmentCenter; [headerLabel setFont:kFont(20)]; [sectionHeaderView addSubview:headerLabel]; switch (section) { case 0: headerLabel.text = @"Section 1"; return sectionHeaderView; break; case 1: headerLabel.text = @"Section 2"; return sectionHeaderView; break; case 2: headerLabel.text = @"Section 3"; return sectionHeaderView; break; default: break; } return sectionHeaderView; } 

我不知道以前版本的UITableView协议,但截至iOS 9, func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?UITableViewDataSource协议的一部分。

  class ViewController: UIViewController { @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() tableView.dataSource = self } } extension ViewController: UITableViewDataSource { func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return "Section name" } } 

您不需要声明delegate来填充表中的数据。

Swift 3:

 override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { let sectionsTitlesKeys = ["Main","New","Options"] let key = sectionsTitlesKeys[section] let localizedString = NSLocalizedString(key,tableName: nil, bundle: Bundle.main, value: "", comment: "") return sectionsTitles[section] } 

我有类似的要求。 我在我的Main.storyboard(Base)中有一个静态的单元格。 要使用.string文件本地化节标题,例如Main.strings(德语),只需select故事板中的部分,并记下对象ID

对象ID

之后去你的string文件,在我的例子Main.strings(德语),并插入翻译如:

 "MLo-jM-tSN.headerTitle" = "Localized section title";