import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
let textValue: NSString = "hellohelloclass ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource "
var myTableView = UITableView()
// 記錄該行numberOfLines狀態
var dict = [String:String]()
override func viewDidLoad() {
super.viewDidLoad()
myTableView = UITableView(frame: self.view.frame, style: .plain)
myTableView.delegate = self
myTableView.dataSource = self
myTableView.tableFooterView = UIView()
self.view.addSubview(myTableView)
// ios 8以后自動適配
myTableView.estimatedRowHeight = 60
myTableView.rowHeight = UITableViewAutomaticDimension
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
cell.textLabel?.text = textValue as String
if dict[String(indexPath.row)] == "0" {
cell.textLabel?.numberOfLines = 0
} else {
cell.textLabel?.numberOfLines = 1
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
tableView.beginUpdates()
if cell?.textLabel?.numberOfLines == 0 {
cell?.textLabel?.numberOfLines = 1
dict[String(indexPath.row)] = "1"
} else {
cell?.textLabel?.numberOfLines = 0
dict[String(indexPath.row)] = "0"
}
tableView.endUpdates()
}
// 分割線頂頭
override func viewDidLayoutSubviews() {
myTableView.separatorInset = UIEdgeInsets.zero
myTableView.layoutMargins = UIEdgeInsets.zero
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero
}
// 返回cell的高度
// func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//
// let arr = [NSFontAttributeName:UIFont.systemFont(ofSize: 17)]
//
// let rect = textValue.boundingRect(with: CGSize(width : 300, height : 0), options: .usesLineFragmentOrigin, attributes: arr, context: nil)
//
// return rect.size.height
// }
}
Cell頂頭分割線
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。