问题
I have some code in my app that expands a cell when it is tapped. The problem is that I haven't figured out how to only expand the cell that has been tapped. At the moment my code just expands all of the cells.
Here is my code:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedRowIndex = indexPath
tableView.beginUpdates()
tableView.endUpdates()
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == selectedRowIndex.row {
if cellTapped == false {
cellTapped = true
return 141
} else {
cellTapped = false
return 70
}
}
return 70
}
What do I need to do to only expand the cell that has been tapped?
回答1:
The following worked for me. The cells are 70 high and expand when tapped :
class ViewController: UITableViewController {
var cellTapped:Bool = true
var currentRow = 0;
Then I slightly modified the following two methods:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var selectedRowIndex = indexPath
currentRow = selectedRowIndex.row
tableView.beginUpdates()
tableView.endUpdates()
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == currentRow {
if cellTapped == false {
cellTapped = true
return 141
} else {
cellTapped = false
return 70
}
}
return 70
}
来源:https://stackoverflow.com/questions/26318209/expand-only-the-cell-that-has-been-tapped