5

I want a table view with only cels, and when you click on a cell it should expand and show more info of the clicked cell.

I've seen quite some topics on this, but the most of them are linking to Table View Animations and Gestures on the apple developer page. Which does it in a different way. They use header sections, but I want to use the cell which is expandable for layout reasons.

I already tried several things mainly with

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {    

    if (isSearching && indexPath.row == selectedIndex) {
        return 110;
    }
    else {
        return rowHeight;       
    }

When I Click on the cell, the cell is expanded but the info in that cell stays the same. Also the heigth of the cell when expanded should be related to the amount of text in the details.

Thnx!

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
peter
  • 51
  • 1
  • 4
  • Did you implement layoutSubviews or use autoresizingMask in the cell ? – David Apr 05 '11 at 11:17
  • yes, cell content is not resized automatically. If you change its height, that won't automatically have effect on its subviews. If an autoresizingMask is set, the subviews will resize accordingly. If the autoresizingMask is not enough, you can override layoutSubviews to programmatically resize subviews. – David Apr 05 '11 at 11:30
  • I have put together a bare bones tutorial about this: http://www.roostersoftstudios.com/site/?p=105 I believe this is what you are talking about and it contains a working iphone project. I hope this helps and let me know if you have any questions -Ryan – rooster117 May 04 '11 at 17:44
  • hi peter i have done something like inserting new custom cell hope this may be helpful i have posted link here http://kshitizghimire.com.np/insert-customuitableviewcell-in-uitablevew-on-row-selected/ test it if it is helpful good luck – Kshitiz Ghimire Apr 05 '11 at 10:34
  • Sound interesting but I don't know if this will get the layout as i want to. Does the extra row not really looks like just another row instead of details? – peter Apr 05 '11 at 11:14
  • you can add extra row as you like just create custom cell , if you want different custom cell per row your need to create different custom cell and load different custom cell for different row as you required – Kshitiz Ghimire Apr 05 '11 at 11:32
  • You going to select an answer for your question? – Brenden Nov 14 '12 at 00:18
  • http://stackoverflow.com/questions/36467993/expand-a-cell-to-show-sub-categories – Tarun Seera Apr 07 '16 at 07:04

1 Answers1

9

You can achieve this through the use of custom cells. Create two custom cells, one for the normal row and other for the expanded row. When the user touches a particular cell, you can record it's indexPath and reload the tableView. While reloading you can change the height of this selected row using the code that you've just posted(increasing the height of only the selected cell). This would give an effect of expanding cell.

Vin
  • 10,517
  • 10
  • 58
  • 71
  • didSelectRowAtIndexPath would be called for the selected cell. You may set up a flag here, which can be referred again by the UITableViewDatSource methods, when a [tableView reloadData] is done. – Vin Apr 05 '11 at 10:25
  • On which way the code knows which custom cell should be selected? Do i declare this in the didSelectRowAtIndexPath? And on which way? – peter Apr 05 '11 at 10:26
  • when the cellForRowAtIndexPath is called again after tableView reload, you can check, using the flag for which cell it was called. You can then redraw the cell with new custom cell, leaving the others like before. – Vin Apr 05 '11 at 10:28
  • Okay! I starting to get it, will try this. Do you by chance also know how I can retract the cell once it is tapped again? – peter Apr 05 '11 at 11:15
  • Once the user touches another cell, the whole process happens again. This time a new cell expands and it looks like the already expanded one is contracting. Try it out!! – Vin Apr 05 '11 at 11:20
  • Ok..but is there also a possibility to close a cell yoursel? For instance with a tap. Because if i get it right there will always be one cell open as soon as you've clicked one. – peter Apr 05 '11 at 11:29
  • I haven't tried that...you'd have to explore that by yourself – Vin Apr 05 '11 at 11:37