如何以编程方式设置UITableView节标题(iPhone/iPad)?
我在Interface Builder中使用故事板创建了一个如何以编程方式设置UITableView节标题(iPhone/iPad)?,iphone,ipad,uitableview,uistoryboard,iboutlet,Iphone,Ipad,Uitableview,Uistoryboard,Iboutlet,我在Interface Builder中使用故事板创建了一个UITableView。UITableView由静态单元格和许多不同的部分设置 我遇到的问题是,我正在尝试用几种不同的语言设置我的应用程序。为此,我需要能够以某种方式更改UITableView部分标题 有人能帮我吗?理想情况下,我希望使用IBOutlets解决这个问题,但我怀疑在这种情况下甚至不可能做到这一点。任何意见和建议都将不胜感激 提前感谢。使用UITableViewDataSource方法 - (NSString *)table
UITableView
。UITableView
由静态单元格和许多不同的部分设置
我遇到的问题是,我正在尝试用几种不同的语言设置我的应用程序。为此,我需要能够以某种方式更改UITableView
部分标题
有人能帮我吗?理想情况下,我希望使用IBOutlets
解决这个问题,但我怀疑在这种情况下甚至不可能做到这一点。任何意见和建议都将不胜感激
提前感谢。使用UITableViewDataSource方法
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
将UITableView委托
和数据源
连接到控制器后,可以执行以下操作:
ObjC
敏捷的
标题ForHeaderInSection是UITableView的一种委托方法,用于按如下方式应用节写入的标题文本:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return @"Hello World";
}
我不知道以前版本的UITableView
协议,但从iOS 9开始,func tableView(tableView:UITableView,titleForHeaderInSection:Int)->String?
是UITableView数据源
协议的一部分
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"
}
}
您无需声明委托
即可用数据填充表格。请注意-(NSString*)表格视图:
如果在UITableView的委托中实现了HeaderInSection:(NSInteger)节
,则UITableView不会调用标题ForHeaderInSection:
如果您正在用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"
}
}
已设置节标签文本
- (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;
}
其他答案没有错,但这一个提供了一个
非编程解决方案,在以下情况下可能有用:
有一个小的静态表。这样做的好处是可以组织
使用故事板进行本地化。可以继续出口
通过XLIFF文件从Xcode进行本地化。Xcode 9还有几个更简单的功能
(原件)
我也有类似的要求。我有一个静态表,在我的Main.storyboard(Base)中有静态单元格。要使用.string文件(例如Main.strings(德语))本地化节标题,只需在情节提要中选择节并记下对象ID
然后转到字符串文件,在我的例子中是Main.strings(德语),并插入如下翻译:
"MLo-jM-tSN.headerTitle" = "Localized section title";
额外资源:
numberOfSectionsInTableView:tableView:
才能调用它。对于静态单元格,(大多数)所有其他数据源方法均未实现。@drewishnumberOfSectionsInTableView:tableView:
在IB中为静态单元格实现。drewish是正确的-如果实现numberOfSectionsInTableView:
,则调用title方法并覆盖故事板。由于这是一个静态的tableview,因此可以使用返回常量的方法覆盖它@wcochranextension
,Swift 5.3似乎不需要它。Xcode生成消息状态:“'SettingsViewController'从这里的超类继承与协议'UITableViewDataSource'的一致性”
- (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;
}
"MLo-jM-tSN.headerTitle" = "Localized section title";