Skip to content

Instantly share code, notes, and snippets.

@anirudhamahale
Last active July 27, 2017 04:57
Show Gist options
  • Select an option

  • Save anirudhamahale/1e6aac74112e7e50486a5a649e387c4a to your computer and use it in GitHub Desktop.

Select an option

Save anirudhamahale/1e6aac74112e7e50486a5a649e387c4a to your computer and use it in GitHub Desktop.
Change the cell's height and attribution depending upon tapping.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ProductOutletsTableViewCell", for: indexPath) as! ProductOutletsTableViewCell
cell.showMoreButton.imageView?.transform = .identity
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == selectedRow {
return 80
}
return 40
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
toggleCellAnimations(tableView: tableView, indexPath: indexPath)
}
var selectedRowIndex = -1
private func toggleCellAnimations(tableView: UITableView, indexPath: IndexPath) {
let previousSelectedRow = selectedRowIndex
if selectedRowIndex == indexPath.row {
// Again the same row is selected.
tableView.beginUpdates()
let cell = tableView.cellForRow(at: indexPath) as? ProductOutletsTableViewCell
if cell?.showMoreButton.imageView?.transform == .identity {
cell?.showMoreButton.imageView?.transform = CGAffineTransform(rotationAngle: .pi)
} else {
cell?.showMoreButton.imageView?.transform = CGAffineTransform.identity
}
selectedRowIndex = -1
tableView.endUpdates()
} else {
// Different row is selected.
tableView.beginUpdates()
if previousSelectedRow != -1 {
let previousCell = tableView.cellForRow(at: IndexPath(row: previousSelectedRow, section: 0)) as? ProductOutletsTableViewCell
previousCell?.showMoreButton.imageView?.transform = .identity
}
let cell = tableView.cellForRow(at: indexPath) as? ProductOutletsTableViewCell
cell?.showMoreButton.imageView?.transform = CGAffineTransform(rotationAngle: .pi)
selectedRowIndex = indexPath.row
tableView.endUpdates()
}
tableView.deselectRow(at: indexPath, animated: true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment