【问题标题】:How to add header to each row in a table view如何为表格视图中的每一行添加标题
【发布时间】:2020-09-04 08:43:42
【问题描述】:

我正在构建一个 iOS 应用,在表格视图中包含一个集合视图。我有三行,每行都有一个集合视图。我计划为每一行的每个部分设置三个部分。例如行,一个应该在一个带有标题的单独部分中,对于第 2 行和第 3 行也是如此。 每当我创建三个部分时,我都会在所有三个部分中获得所有三行。我想有一个单独的部分,每行都有一个标题。

import UIKit

class StoreVC: UIViewController,UITableViewDelegate,UITableViewDataSource {


    @IBOutlet weak var CourseTableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        CourseTableView.tableFooterView = UIView()
        
        CourseTableView.delegate = self
        CourseTableView.dataSource = self
    }
    
   func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if section == 0
        {
            return "Courses"
        }
        
        else if section == 1
        {
            return "Tests"
        }
        
        return "Bundles"
    }
    
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      return 1
    }
    

    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       
        
        if indexPath.row == 0
        {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CourseRow
            
            return cell
        }
        
        else if indexPath.row == 1
        {
            let cell = tableView.dequeueReusableCell(withIdentifier: "testcell", for: indexPath) as! TestRow
            
            return cell
        }
        
        else if indexPath.row == 2
        
        {
            let cell = tableView.dequeueReusableCell(withIdentifier: "bundlecell", for: indexPath) as! BundleRow
            
            return cell
        }
        
        return UITableViewCell()
        
        
        
    }
    
    
    


}

【问题讨论】:

  • numberOfSections(in...) { return 3 } 是 tableViews 的快速方法。然后在你的tableView(cellForRowAt..) { if section == 1 { //doSomething } else if section == 2 { //doSomething } else { //doSomething } 最后设置numberOfRowsInSection to return 1

标签: ios swift uitableview uicollectionview uitableviewsectionheader


【解决方案1】:

在您的 Xcode-playground 上试用此代码并根据需要进行自定义。 导入 UIKit 导入 PlaygroundSupport

class ViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        3
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        1
    }

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UILabel()
        headerView.text = "Header: \(section)"
        return headerView
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.text = "Cell: \(indexPath)"
        return cell
    }
}

PlaygroundPage.current.liveView = ViewController()

【讨论】:

  • 不,它不起作用。我在所有三个部分中获得第一行,其余两个部分不可用。我已经添加了我的代码。
  • 我根据你的代码修改了我的应用程序代码,但我不工作
  • 反过来试试。将此张贴在操场上,根据您的要求进行更改。
  • 谢谢我明白了我在 if 语句中将 index.row 替换为 index.section 现在可以正常工作了
猜你喜欢
  • 2014-05-07
  • 1970-01-01
  • 1970-01-01
  • 2020-10-17
  • 2011-09-02
  • 2020-12-22
  • 2013-07-31
  • 1970-01-01
  • 2015-07-05
相关资源
最近更新 更多