【问题标题】:UITableView get titleForHeadersInSection swiftUITableView 快速获取 titleForHeadersInSection
【发布时间】:2015-01-16 15:12:56
【问题描述】:

我想在 UITableView 的部分设置表头的标题。 swift 中设置部分标题标题的语法是什么。

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

【问题讨论】:

  • 为什么不使用章节编号而不是标题?
  • 其实我想知道获取标题For Header In Section的语法。

标签: uitableview swift swift-playground ios8.1


【解决方案1】:

您可以使用已在您的类中定义的函数,即:

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
}

【讨论】:

  • 谢谢。顺便说一句,你不需要“休息”。
  • 删除了'break's - 从一个较大的sn-p的剪切和粘贴中留下的:)
【解决方案2】:

为了调用这个方法,你必须使用 UITableViews titleForHeaderInSection 方法。该方法会提供当前section的索引,需要返回一个String,返回的String会被设置为header。

为了调用它,假设我们有一个名为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"
}

这将按上面给出的顺序返回章节标题。

【讨论】:

    【解决方案3】:

    更新 Swift 5

    Xcode - 11.4

    func tableView( _ tableView : UITableView,  titleForHeaderInSection section: Int)->String? {
       switch(section) {
         case 1:return "Title of section header"
         default :return ""
       }
    }
    

    这将在大写锁定中显示标题。要使其以常规模式显示,请使用此方法以及上述方法:

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

    【讨论】:

      【解决方案4】:
      func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
              if section == 0{
                 return "Pass Your String For Header"
             }else{
                return "Pass Your String For Header"
           }
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-06-29
        • 1970-01-01
        • 1970-01-01
        • 2016-09-03
        • 2013-03-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多