问题
As the title says, I'd like to make my cells dynamically change height. I try to be as clear that I can. When I touch up inside a cell (prototype cell), the cell change height (from 44 to 88);in the cell there is an UIImage (88px height) partially hidden and where the cell is hidden there is view, touching up starts a segue to a second tableView; when I touch up inside an other cell the previous cell return to 44px height an this other cell change height as the previous one. Follow a scheme of I need to do.
I think that this is quite difficult, so If someone could help me doing this feature I would be very grateful!! or if you know any examples to get ideas (by gitHub for example) please report me! thanks a lot :-)
回答1:
It is not so bad. I have done it like this, there might be smarter ways to do it. The important stuff is tableView.beginUpdate and ....endUpdate.
//
// DynamicTableViewController.swift
// DynamicCellTest
//
// Created by Lars Christoffersen on 27/08/15.
// Copyright (c) 2015 Lars Christoffersen. All rights reserved.
//
import UIKit
class DynamicTableViewController: UITableViewController {
var dataCource = ["A","B","C","D","E","F","G","H"]
var extraHeight:CGFloat?
var selectedRowIndex:Int?
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if extraHeight != nil && selectedRowIndex != nil && selectedRowIndex == indexPath.row{
return 41 + extraHeight!
}else{
return 41
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.tableView.beginUpdates()
selectedRowIndex = indexPath.row
extraHeight = 100
self.tableView.endUpdates()
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
self.tableView.beginUpdates()
selectedRowIndex = indexPath.row
extraHeight = 0
self.tableView.endUpdates()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataCource.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = dataCource[indexPath.row]
return cell
}
}
来源:https://stackoverflow.com/questions/32238174/change-uitableviewcell-height-touch-up-inside