Ho un imageView su un UITableViewCell
e voglio impostare l'altezza della cella / imageView su un valore diverso confrontare ciò che ho impostato nella cella prototipo. Non voglio implementare heightForRowAtIndexPath
metodo, perché non voglio precalcolare l'altezza del contenuto. Se UITableViewAutomaticDimension è impostato e almeno su iOS 8 heightForRowAtIndexPath
non ha bisogno di implementare.
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 3
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell")!
let iv = cell.viewWithTag(1) as! UIImageView
iv.image = UIImage(named: "img1")
let cs = iv.constraints.first! as NSLayoutConstraint
cs.constant = 100
cell.layoutIfNeeded()
cell.setNeedsUpdateConstraints()
return cell
}
Struttura del prototipo:
Sebbene l'altezza della cella nel prototipo sia impostata su 44,0 px, ma in cellForRowAtIndexPath
metodo Voglio sovrascrivere questo valore con 100.0 px.
risposte:
0 per risposta № 1Non funzionerà come fai tu.
È necessario implementare il metodo delegato di UITableViewDelegate
func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
// calculate size of image and return its height here
}