swift cell的自定义高度

2018-01-05  本文已影响0人  刘波_ecae

//创建各单元显示内容(创建参数indexPath指定的单元)

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)

        -> UITableViewCell {

            let identify:String = "Cell"

            var cell:VisitListTableViewCell? = tableView.dequeueReusableCell(withIdentifier: identify)

                as? VisitListTableViewCell

            if(cell == nil){

                cell = VisitListTableViewCell(style: .default, reuseIdentifier: identify)

            }

            if dataRecordList.count > 0 {

                let dataRecord = dataRecordList[indexPath.row]

                cell?.creatorLabel?.text = "记录人:\(dataRecord.creator)"

                cell?.timeLabel?.text = "日期:\(dataRecord.createDate)"

                cell?.nexttimeLabel?.text = "\(dataRecord.intervalDays)"//间隔时间

                cell?.nexttime?.text = "下次拜访时间:\(dataRecord.nextPlanDate)"//时间nexttime

                cell?.webview.text = "\(dataRecord.content)"//.loadHTMLString(content, baseURL: nil)

                cellheight = cellhight(content: "\(dataRecord.content)")

                cell?.webview.frame = CGRect(x:15, y:24, width:width-90, height:cellheight)

            }

            return cell!

    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)  {

        let dataRecord = dataRecordList[indexPath.row]

        let vc = VisitDetailsViewController()

        let nav = UINavigationController(rootViewController:vc)

        vc.vid = dataRecord.vid

        self.present(nav, animated: true, completion: nil)

    }

    @objc func cellhight (content: String) -> CGFloat {

        let rect: CGRect = content.boundingRect(with: CGSize(width:width-135, height:0),options: NSStringDrawingOptions.usesLineFragmentOrigin,attributes: [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 14)], context: nil)


        return rect.height + 25

    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        let dataRecord = dataRecordList[indexPath.row]

        return cellhight(content: dataRecord.content)+40

    }

上一篇下一篇

猜你喜欢

热点阅读