18

I want to expand the row height and show the content inside. I show my content in view I want when I tap on a cell it should expand like showed in the image below but without reloading the UITableView.

IMAGE

What I have tried till now

  1. I tried expanding the view inside but it didn't work

  2. I tried adding rowAtIndex it was becoming complicated

  3. I tried changing change row height and scroll at index it worked fine but I don't want like that. (bad thing I have to reload the table view)

But I got many answers but it didn't address my issue as my data in the array(data source) is continuously updated if I refresh at specific index the data in the array at the index might be different it might show the wrong output.

Explanation

I mean I have an array of struct I keep on updating it in background once the data change and reload the table view but if the i reload the row at index and if the data in an array is already changed it might show duplication correct? so i want to just change the height of the row without doing any reload.

Problem with reloading is

while expanding the data might change in the array. so i already created a view and all the info is preloaded once i tap the height of the cell should change from 45 to 76 and once i tap different cell again from last cell 76 to 45 and this cell 45 to 76.

אורי orihpt
  • 2,358
  • 2
  • 16
  • 41
O-mkar
  • 5,430
  • 8
  • 37
  • 61

6 Answers6

25

You don't even need to reload the cell, just get the current cell at the target location and modify it directly and call beginUpdates and endUpdates on the table view to have it recalculate all of the row heights.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • I don't understand what you mean about your source data array always changing - if it changes you need to reload the table in some way, and you need to store the expanded status in your data model too... – Wain May 02 '16 at 07:45
  • I mean I have an array of struct I keep on updating it in background once the data change and reload the table view but if the i reload the row at index and if the data in array is already changed it might show duplication correct? so i want to just change the height of the row without doing any reload – O-mkar May 02 '16 at 07:50
  • and my answer relates to updating the table row height without any reload, but your data array needs to contain the information that it has been expanded so your delegate returns the correct height for that row index path – Wain May 02 '16 at 07:52
  • Yeah that is the problem while expanding the data might change.. so i already created a view and all the info is preloaded once i tap the height of the cell should change from 45 to 76 and once i tap different cell again from last cell 76 to 45 and this cell 45 to 76 – O-mkar May 02 '16 at 07:55
  • so the delegate supplies the height and it needs to know that the height has changed, if only the height has changed then you can use my answer to update the UI, if the data array changes then you need to reload – Wain May 02 '16 at 08:04
  • I don't reload the table once the data is changed instead i show button on top new feedback tap to reload ... – O-mkar May 02 '16 at 08:14
  • So you need 2 different arrays to manage that so you separate the current data on the table from the data waiting to be loaded / shown – Wain May 02 '16 at 08:29
  • if we have a callectionview in out tableviewcell then reloading cause callectionview scroll reset what we can do in this situation if we use begin and end updates it will not use accurate animation and in scrolling when the cell is bottom of page some weird changing happen – Hamed Nova Dec 13 '17 at 09:24
  • 1
    @nova that's a separate question. your collection view should have its own view controller to deal with state persistence – Wain Dec 13 '17 at 11:49
  • you mean i have to create another controller as collectionView container and pass them to tableview cell each time the table cell is reloading? – Hamed Nova Dec 14 '17 at 11:44
21

You can adjust UITableView height and also manage reloading using below code

self.tableView.beginUpdates()

self.tableView.endUpdates()

enter image description here

Community
  • 1
  • 1
Sheereen S
  • 1,292
  • 10
  • 18
  • 1
    Thanks for this. Just these two lines solved my issue of being stuck in a loop. I've been tinkering for hours now. No need for reloadRows or reloadSection inside these two update lines. – Glenn Posadas Dec 18 '18 at 13:46
  • 1
    Thank you, this is exactly what I needed to trigger cell resizing without reloading the cell data. Put it in the delegate method before the delegate updated the content that was the reason for resizing, and worked perfectly. – user7804781 Aug 16 '19 at 21:24
7

Table height can only be changed through delegates there is no other way to do it i will show you a simple way to do it.

Declare variable

var selectedIndex = NSIndexPath()

Inside didSelectRowAtIndexPath reload selected cell

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        selectedIndex = indexPath
        tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)

    }

Inside heightForRowAtIndexPath return size

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath == selectedIndex {

        return 100 //Size you want to increase to
    }
    return 50 // Default Size
}
6

You can try with 2 custom cells. track user selection with variable and reload single cell with below code. when user click on cell load Detailed cell in cellForRowAtIndexPath. your tableView array should contain information which you are showing when cell is expended.

self.tableView?.beginUpdates()
var indexPath = NSIndexPath(forRow: rowNumber, inSection: 0)
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)
self.tableView?.endUpdates()
iVarun
  • 6,496
  • 2
  • 26
  • 34
4

Supposing that the logic to expand your TVC is working, you just need to call table view methods beginUpdates and endUpdates. Those method should wrap your update to the TVC.

Andrea
  • 26,120
  • 10
  • 85
  • 131
2

You don't have to reload the whole table. UITableView has a method func reloadRowsAtIndexPaths(indexPaths: [NSIndexPath], withRowAnimation animation: UITableViewRowAnimation). You can use this to update a single cell.

Jelly
  • 4,522
  • 6
  • 26
  • 42
  • 1
    if we have a callectionview in out tableviewcell then reloading cause callectionview scroll reset what we can do in this situation if we use begin and end updates it will not use accurate animation and in scrolling when the cell is bottom of page some weird changing happen – Hamed Nova Dec 13 '17 at 09:24