UITableView进阶
1.2.1UITableViewCell点击内容扩展效果(如下图所示)

1.2.2实现原理
点击之后单元格Label的linesofnumber变量改变,重新加载即看到不同数量的lines
1.2.3源码
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
var cellText = "hello world\nhello world\nhello world\nhello world\nhello world\nhello world\nhello world\n"
@IBOutlet weak var tableView: UITableView!
//这是一个字典变量,用来存放label的lines值
var dict:Dictionary<String,String> = [:]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView.estimatedRowHeight = 60
tableView.rowHeight = UITableViewAutomaticDimension
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 100
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath)
let label = cell.contentView.viewWithTag(100) as! UILabel
label.text = cellText
if dict[String(indexPath.row)] == "0"{
label.numberOfLines = 0
}else{
label.numberOfLines = 1
}
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
let label = cell?.contentView.viewWithTag(100) as! UILabel
//beginUpdates()和endUpdattes()之前的代码会形式一个帧播放效果,就像实现动画一样
tableView.beginUpdates()
if label.numberOfLines == 0{
label.numberOfLines = 1
dict[String(indexPath.row)] = "1"
}else{
label.numberOfLines = 0
dict[String(indexPath.row)] = "0"
}
tableView.endUpdates()
}
}
浙公网安备 33010602011771号