按表索引单元格数据

时间:2018-01-17 10:04:09

标签: ios iphone swift

我已经实现了启用分页的表视图,并且单元格大小是设备屏幕大小,我想要做的是在完全滚动时通过表格单元格的索引数据更改导航栏标题

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    let doseData = topDoseData[indexPath.row]
    self.navigationItem.title = doseData.value(forKey: "posted_on") as? String
}

2 个答案:

答案 0 :(得分:0)

如果一切都按预期设置,则问题可能出在您的数据中。您可以打开可选值来查找错过的位置。你可以尝试

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    guard let navigationController = self.navigationController else {
        fatalError("navigationController has not been set yet")
    }

    let doseData = topDoseData[indexPath.row]

    guard let title = doseData.value(forKey: "posted_on") as? String else {
        fatalError("Couldn't find data with key: posted_on")
    }
    navigationController.title = title
}    

我将fatalError仅用于调试目的。

答案 1 :(得分:0)

通过获取可见单元格来更改页面标题

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    for cell in dosetable.visibleCells {
    let indexPath = dosetable.indexPath(for: cell)
    let doseData = doseArry[(indexPath?.row)!]
    let post_interests = doseData.value(forKey: "post_interests") as! [AnyObject]
    let bttnImgUrl = post_interests[0].value(forKey: "image") as? String
    let intrstTag = post_interests[0].value(forKey: "id") as! String
    let postIntrest = doseData.value(forKey: "post_interests") as! [AnyObject]
    bttnImg.kf.setImage(with: URL(string: bttnImgUrl!))
    intrestTitle.setTitle(postIntrest[0].value(forKey: "name") as? String, for: .normal)
    intrestTitle.addTarget(self, action: #selector(intrestBttn(sender:)), for: .touchUpInside)
    intrestTitle.tag = Int(intrstTag)!
    intrestBttn.tag = Int(intrstTag)!
    intrestBttn.addTarget(self, action: #selector(intrestBttn(sender:)), for: .touchUpInside)
    timeAgo.text = NSLocalizedString((doseData.value(forKey: "posted_on") as? String)!, comment: "")
    intrestBttn.setBackgroundImage(bttnImg.image, for: .normal)
    }
}