在单元格外显示标签(tableViewCell)

时间:2017-10-05 11:55:26

标签: ios swift function label

是否可以在单元格外显示 tableViewCell 标签?即,默认情况下,标签出现在函数

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

    cell2.currentPageNumber.text = "1/\(Images?.Photo.count ?? 0)"

return cell2

我想在此函数中显示currentPageNumber.text

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

    let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)

    currentPageNumber?.text = "\(String(Int(pageNumber) + 1))/\(Images?.Photo.count)"
}

tableViewCell

class DetailTableViewCell2: UITableViewCell {

    @IBOutlet weak var currentPageNumber: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()

        currentPageNumber.layer.zPosition = 2
        currentPageNumber.layer.cornerRadius = 15.0
        currentPageNumber.clipsToBounds = true

    }

}

有可能吗?如果可能的话,怎么样?

2 个答案:

答案 0 :(得分:1)

这里有可见的单元格IndexPath

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

//replace your cell class and tableView Object
        for cell in tableView.visibleCells {
            let indexPath: IndexPath? = tableView.indexPath(for: cell)
            print("\(indexPath)")
        }
    }

答案 1 :(得分:0)

这对我很有帮助

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

    let indexPath = IndexPath(row: 0, section: 0)

    if let cell = tableView.cellForRow(at: indexPath) as? DetailTableViewCell2 {

        let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)

        cell.currentPageNumber.text = "\(String(Int(pageNumber) + 1))/\(hallImages?.ImagesHalls.count ?? 0)"

    }

}