UITableView获取标题标题标题版本swift
我想在UITableView的部分中设置标题。swift中设置节标题的语法是什么UITableView获取标题标题标题版本swift,uitableview,swift,swift-playground,ios8.1,Uitableview,Swift,Swift Playground,Ios8.1,我想在UITableView的部分中设置标题。swift中设置节标题的语法是什么 func tableView( tableView : UITableView, titleForHeaderInSection section: Int)->String { switch(section) { case 2: return "Title 2" break default: return ""
func tableView( tableView : UITableView, titleForHeaderInSection section: Int)->String
{
switch(section)
{
case 2:
return "Title 2"
break
default:
return ""
break
}
}
func tableView (tableView:UITableView , heightForHeaderInSection section:Int)->Float
{
var title = tableView.titleForHeaderInSection[section];
if (title == "") {
return 0.0;
}
return 20.0;
}
func tableView (tableView:UITableView, viewForHeaderInSection section:Int)->UIView
{
var title = tableView.titleForHeaderInSection[section] as String
if (title == "") {
return UIView(frame:CGRectZero);
}
var headerView:UIView! = UIView (frame:CGRectMake(0, 0, self.tableView.frame.size.width, 20.0));
headerView.backgroundColor = self.view.backgroundColor;
return headerView;
}
要调用此方法,必须使用UITableView
titleForHeaderInSection
方法。此方法将为当前节提供索引,您需要返回一个字符串,返回的字符串将设置为标题
为了调用它,我们假设有一个名为cars
cars = ["Muscle", "Sport", "Classic"]
然后我们就可以打电话了
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
// Ensure that this is a safe cast
if let carsArray = cars as? [String]
{
return carsArray[section]
}
// This should never happen, but is a fail safe
return "unknown"
}
这将按上述顺序返回章节标题。您可以使用类中已定义的func,即: self.tableView(tableView,titleForHeaderInSection:section) 例如,使用您的代码:
func tableView( tableView : UITableView, titleForHeaderInSection section: Int)->String {
switch(section) {
case 2:return "Title 2"
default :return ""
}
}
func tableView (tableView:UITableView , heightForHeaderInSection section:Int)->Float
{
var title = self.tableView(tableView, titleForHeaderInSection: section)
if (title == "") {
return 0.0
}
return 20.0
}
更新Swift 5 Xcode-11.4
func tableView( _ tableView : UITableView, titleForHeaderInSection section: Int)->String? {
switch(section) {
case 1:return "Title of section header"
default :return ""
}
}
这将在caps lock中显示标题。要使其在常规模式下显示,请将此方法与上述方法一起使用:
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let titleView = view as! UITableViewHeaderFooterView
titleView.textLabel?.text = "Title of section header"//titleView.textLabel?.text?.lowercased()
}
为什么不使用节号来比较而不是标题呢?实际上我想知道在节中获取标题的语法。谢谢。顺便说一句,你不需要“中断”。删除了“中断”-从一个较大片段的剪切和粘贴中留下的:)