日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Swift之点击UITableView单元格动态改变cell高度

發(fā)布時(shí)間:2024/5/21 编程问答 150 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Swift之点击UITableView单元格动态改变cell高度 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

基于上一篇文章,繼續(xù)需要實(shí)現(xiàn)點(diǎn)擊相應(yīng)的表格單元格動(dòng)態(tài)改變cell的高度(上一篇文章的地址Swift之動(dòng)態(tài)適配UITableView的cell高度)

  • 首先需要實(shí)現(xiàn)UITableView的tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)協(xié)議;
  • 其次,需要一個(gè)字典記錄已經(jīng)點(diǎn)擊的單元格,從而再次點(diǎn)擊單元格刷新表格視圖;
  • 改寫tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell協(xié)議。
var middleDict:Dictionary<String,String> = [:] // 記錄已經(jīng)點(diǎn)擊的單元格func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {var cell:PoemTableViewCell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! PoemTableViewCellif cell .isEqual(nil) {cell = PoemTableViewCell.init(style: .default, reuseIdentifier: identifier)}cell.showLabel.text = textArray[indexPath.row] as? Stringcell.selectionStyle = .none// 改變showLabel的numberOfLinesif middleDict[String(indexPath.row)] == "0" {cell.showLabel.numberOfLines = 0} else {cell.showLabel.numberOfLines = 1}return cell}// 實(shí)現(xiàn)didSelectRowfunc tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {let cell:PoemTableViewCell = tableView.cellForRow(at: indexPath) as! PoemTableViewCellif cell.showLabel.numberOfLines == 0 {cell.showLabel.numberOfLines = 1middleDict[String(indexPath.row)] = "1"} else {cell.showLabel.numberOfLines = 0middleDict[String(indexPath.row)] = "0"}self.myTableView.reloadData() }
  • 點(diǎn)擊cell之后,動(dòng)態(tài)改變cell高度還可以添加動(dòng)畫效果,只需要替換self.myTableView.reloadData()為self.myTableView.beginUpdates()和self.myTableView.endUpdates();
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {let cell:PoemTableViewCell = tableView.cellForRow(at: indexPath) as! PoemTableViewCellself.myTableView.beginUpdates()if cell.showLabel.numberOfLines == 0 {cell.showLabel.numberOfLines = 1middleDict[String(indexPath.row)] = "1"} else {cell.showLabel.numberOfLines = 0middleDict[String(indexPath.row)] = "0"}self.myTableView.endUpdates() // self.myTableView.reloadData()}
  • 最后再跟大家分享一個(gè)小技巧:UITableView的分割線默認(rèn)是開頭空15像素點(diǎn),當(dāng)我們需要頂格顯示時(shí),只需要重載viewDidLayoutSubviews()方法和實(shí)現(xiàn)tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)即可;
override func viewDidLayoutSubviews() {self.myTableView.separatorInset = .zeroself.myTableView.layoutMargins = .zero}func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {cell.separatorInset = .zerocell.layoutMargins = .zero}

總結(jié)

以上是生活随笔為你收集整理的Swift之点击UITableView单元格动态改变cell高度的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。