ios - UITableView 中的单元格标题

标签 ios swift uitableview

我有一系列这样的任务:

68  -  Incontro  -  Incontro  -  10/07/2017  -  Incontro robot
69  -  Compito  -  Matematica  -  11/07/2017  -  Pag 620 n.19
71  -  Incontro  -  Incontro  -  11/07/2017  -  
70  -  Interrogazione  -  Matematica  -  12/07/2017  -  da pag 200 a pag 230

其中第一个参数是 ID,第二个是类型,第三个是主题,第四个是日期,最后一个是评论。

我想要一个 TableView ,它显示我的数组的所有元素,每个元素都在一个单元格中,带有名称和注释,我希望所有单元格都有一个带有日期的标题。 我的课是这样的:

class TasksViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return MenuViewController.tasksArray.count
    }

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

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 45
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 10
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let  headerCell = tableView.dequeueReusableCell(withIdentifier: "headerCell") as! CustomHeaderCellTableViewCell
        headerCell.backgroundColor = UIColor.cyan

        headerCell.headerLabel.text = MenuViewController.tasksArray[0].date

        return headerCell
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        tableView.rowHeight = 70
        cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator

        cell.backgroundColor = funzioni.hexStringToUIColor(hex: "#e5e5ec")

        cell.textLabel?.text = MenuViewController.tasksArray[indexPath.row].subject
        cell.detailTextLabel?.text = MenuViewController.tasksArray[indexPath.row].comment

        return cell
    }
}

我的问题是每次都显示第一个任务。 当我在 tableView 中向下移动时,我也不想移动标题,如图所示:

enter image description here

最佳答案

首先,你总是显示第一个任务,因为你总是通过这段代码请求第一个任务

headerCell.headerLabel.text = MenuViewController.tasksArray[0].date

为了使它正确,您需要根据您的单元格的当前索引获取任务,这意味着

headerCell.headerLabel.text = MenuViewController.tasksArray[section].date

其次,要让header跟在你的cell后面,你需要设置table view的样式来分组,像这样

enter image description here

对于您的标签,更改以下代码

cell.textLabel?.text = MenuViewController.tasksArray[indexPath.row].subject
cell.detailTextLabel?.text = MenuViewController.tasksArray[indexPath.row].comment

进入

cell.textLabel?.text = MenuViewController.tasksArray[indexPath.section].subject
cell.detailTextLabel?.text = MenuViewController.tasksArray[indexPath.section].comment

每个部分有一个单元格。因此,每个部分中的行始终为 0。这就是为什么您一遍又一遍地获得相同标签的原因。

关于ios - UITableView 中的单元格标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45013226/

相关文章:

ios - 如何将字符串转换为数据

swift - 在 Swift 中使用 NSUserDefaults 保存和检索 TableViewCell 复选标记

swift - 使用 NSUserDefaults 在 ViewController 之间传递数据

ios - 类型 'Any?' 的值没有成员 'tag'

ios - 在之前的详细信息披露之上加载详细信息披露

ios - 一次移动多个对象 - iOS

ios - 如何测试 iTunesConnect 等 iOS 应用程序?

swift - 为什么使用 NetFSMountURLSync 的命令行参数中的瑞典语字符不起作用?

ios - bool _WebTryThreadLock(bool) 崩溃

Swift tableView setContentOffset 不会反弹