0

I'm looking for a way to change a specific row in my tableView.

I'm using a Notification to check when I do an action in my cell. According to the answer, my goal is to display the next row.

By default, my cell have this property.

if (indexPath.row == 5){
    tableView.rowHeight = 0.0
}

if (indexPath.row == 6){
    tableView.rowHeight = 0.0
}

return cell

My goal when I'm in my notification is to change the row height value for the fifth row.

Thank you for your help

Paulw11
  • 108,386
  • 14
  • 159
  • 186
noxo.
  • 116
  • 10
  • Thanks but No because I would like to change the height of the row directly from my notification, not in the tableView function – noxo. Feb 10 '20 at 22:58
  • You need to change cell row on click action? – Nagarjun Feb 10 '20 at 23:00
  • Yes it's my goal. In my app, I'm using radio items so if I choose one specific item, I want to display the next one – noxo. Feb 10 '20 at 23:01

1 Answers1

2

You could use a Set<IndexPath> and your tableView delegate methods to achieve this.

Say you have a set of selected index paths selectedIndexPaths and heights largeHeight and normalHeight. Your heightForRow func could look like this:

func tableView(_ tableView: UITableView, heigthForRowAt indexPath: IndexPath) -> CGFloat {
    guard !selectedIndexPaths.contains(indexPath) else {
        return largeHeight
    }

    return normalHeight
}

Then you could change the height dynamically the following way:

/// Convenience method for selecting an index path
func select(indexPath: IndexPath, completion: ((Bool) -> Void)? = nil){
    selectedIndexPaths.insert(indexPath)
    tableView.performBatchUpdates({
        self.tableView.reloadRows(at: [indexPath], with: .none)
    }, completion: completion)
}

In your tableView delegate, you could call this method in didSelect:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    select(indexPath: indexPath)
}

Do the same if you have a method responding to your notification (assuming you placed your indexPath in your notification's userInfo, under the key "indexPathKey"):

func notifiedShouldEnlargeRow(aNotification: Notification) {
    guard let indexPath = aNotification.userInfo["indexPathKey"] as? IndexPath else { return }
    select(indexPath: indexPath)
}

For reference, look at performBatchUpdates(_:completion) and reloadRows(at:with:).

kid_x
  • 1,415
  • 1
  • 11
  • 31
  • Not all of the code was necessary but I found the solution with ```self.tableView.reloadRows(at: [indexPath], with: .none) ``` Thank you – noxo. Feb 12 '20 at 10:54
  • 1
    Just thought I'd drop a full working example. :) Glad it helped! – kid_x Feb 12 '20 at 15:57