expandable and collapsable uitableviewcell [duplicate] - iphone

This question already has an answer here:
Table view cell expandable iOS
(1 answer)
Closed 9 years ago.
I want to achieve something like an expandable and collapsible UITableViewCell. The part of collapsing and expanding on user tap has been achieved, but what i am looking for is the feature of showing more and less.
That is, when the UITableViewCell has not expanded to show full view, the word "more" should be displayed. When the UITableViewCell is expanded, it should show the word "less" at the end. So whenever the user taps on more, then only the UITableView should expand and vice versa. Any ideas on how I could do this? Something like on web pages. When u click on more link it expands the area and lets u see its entire content.

Expanding on iWasRobbed's answer, you are looking for WWDC 2010 Session 128, Mastering Table Views, which you can officially find at apple.com.
It includes a demo of expanding table cells by updating the values returned by tableView:heightForRowAtIndexPath: then calling:
[tableView beginUpdates];
[tableView endUpdates];
on the UITableView. Since you don't need to change which rows are shown, you don't need to include any code between begin and end. Once endUpdates is called, the heights of all the rows are re-calculated by calls to tableView:heightForRowAtIndexPath: for each row.
The video includes examples of how to animate the updates, including how to disable the animations.

Related

iPhone - Smooth animation for UITableViewCell height change including content update [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Can you animate a height change on a UITableViewCell when selected?
I have a table view into which where you click on a swich into a cell, many cells of the table view may change their height. It's a little bit complicated to compute which cells will change their size, so I'd like not to call twice the same code.
I have a heightForRowAtIndexPath method (this is the complicated one).
Is there a way to animate the cell grow or reduce feature without impacting the one that does not change ?
I've tried UIView Animations, reloadData for sections with animation, ... but I didn't find anything smooth.
I've tried to catch each cell that will have to change and call a reloadData with animation on each cell. It's near to work but the cells are updating one after the other. And as I said before, I don't want to do that because I must call twice the heightForRowAtIndexPath method.
What I'd like is something that can animated each cell that has a changing height with somthing like a grow or reduce movement, and not a fade one.
Do you know a way to do that ?
One more thing, the content of the cell is changing a little bit when size change (some text added, back color changing, and things like that)
UITableView has a neat trick to animate changes in cell heights: if you send an empty updates block to the table view, it won't insert or delete any cells, but it will still recalculate cell heights for the entire table. So even though this piece of code looks weird, it should do it:
[tableView beginUpdates];
[tableView endUpdates];
If you want to change the contents of the updated cells, too, you must add one or more calls to reloadRowsAtIndexPaths:withRowAnimation: to the updates block, of course.

Left area of section in iPhone TableView

I have a section where I would like to customize the left area of a section a TableView - a bit like viewForHeaderInSection.
I have thought at using a cell for the section instead, but it would be a lot of nitty-gritty.
If I understand your question correctly, you want to customise the left-hand-side of some, or every cell in a UITableView? Then you need to make create your own custom table view cells. I normally make these cells in Interface Builder; this post helped me out. See also the Customizing Cells section of Apple's Table View Programming Guide for iOS.
I read your question to mean that there is one custom element to the left of a bunch of cells. The only way I know of offhand is to use a cell as you describe and then have a left view and a tableview inside of it.

IPhone - Get reference to cells that are currently on screen

I have a Table View Controller with cells. I want to update the text that is displayed on the visible UITableViewCells. Is there a way to get a reference to the cells that are currently on the screen?
You can query the visible cells;
[myTableOutlet visibleCells];
Where myTableOutlet refers to your UITableView, and visibleCells returns an array of table cells. You could also use indexPathsForVisibleRows, which will return an array of index paths for the rows that are currently visible.
If you put the code in a ,notification triggered, function, you could update only the currently visible cells.
What you can do is go and ask for the cells at index paths using - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath, if they are not nil, you know they are on the screen and you can do what you wish with them. Also if you know the visible rectangle on the screen (which shouldnt be hard to get) you can use tableViews - (NSArray *)indexPathsForRowsInRect:(CGRect)rect and you can get all your visible index paths and use cellForRow in order to get the cells back.
As long as this is marked as answer, I stand corrected, like others said you can do [tableView visibleCells]
Call the reloadData method of the UITableView. It will force the table view to update all the visible cells information (check the documentation for more information).

Reordering UITableView sections on the iPhone by dragging (like reordering rows)

Is there anything currently in the SDK that allows for re-ordering of tableView sections? As in, move the entire section below or above an adjacent section? This is plausible with individual UITableViewCells. Haven't seen it done for sections though.
Maybe not ideal but fairly simple so worth considering:
Add Up/Down buttons to your section headers. Down on the first, Up on the last and Up and Down on all the others. Then respond to the button presses by programmatically re-ordering the table. Up moves the section up one and Down moves the section down one.
Martin
There's no built-in touch-responsive API for moving table view sections - you'd have to do it programmatically then send a [tableView reloadData] message or similar.
It is concievable, though, that you create a table view where each UITableViewCell's view is itself a UITableView containing a section of your data, so that the cells in the "master" table are draggable as UITableViewCells. This would let you reorder "sections" in your table, but they wouldn't be sections anymore - they'd be separate tables, each with a single section.
Maybe as an alternative idea, reloadData on the table to only show the sections as a kind of "collapsed all" view on your table (maybe it is possible to even animate this?) which then has one section, containing all sections as row elements. The only extra required would then be a button that will set your table to this state.
I tried to move rows from one section to another, while updating the datasource of course. But this seemed to be mission impossible. Empty cells in between, wrong contents in sections, wrong ordering of sections were the only results I got.

Redoing UITableView layout when user taps Edit button?

I have a UITableView with complex content. The user can edit (rearrange and delete) the cells when tapping the Edit button the standard way. But I want the cells to look different in "edit" mode.
Question:
How to change the UITableView Layout in edit mode, including changing row height?
So far, this is what I have:
The Edit button sends a WillTransitionToState/DidTransitionToState message to each uitableviewcell (UITVC). I have subclassed UITVC and react to these inside each cell, hiding and removing and reshuffling as needed. But, changing the row height is beyond the scope of one cell.
There does not seem to be a message sent to UITableView when user taps edit. There is a - tableView:commitEditingStyle:forRowAtIndexPath: sent to data source after editing a particular row.
Inside heightForRowAtIndexPath, I can query the current mode using the tableView.editing property, and report height as appropriate. And I can trigger re-flowing the table, including recomputing the heights, by invoking [tableView reloadData]. But, when do I call it?
I could send messages from the cells from within WillTransitionToState back to the "owning" table view, and call reloadData when I get them. But this sounds fragile and there must be a better way.
Rhythmic is right. Using reloadData kills the nice editing animation.
This problem is addressed in this post:
Can you animate a height change on a UITableViewCell when selected?
Instead of using reloadData, do the following after calling setEditing:animated.
[tableview setEditing:editing animated:YES];
[tableview beginUpdates];
[tableview endUpdates];
If you wish for your table cells to change their format in response to whether or not the table is in editing mode, you could override -setEditing:animated: in your UITableViewController and trigger a reload (via -reloadData) of the table view on a change of editing state.
Within your UITableViewController's -tableView:cellForRowAtIndexPath: method, you could check for whether or not the table was in the editing state by querying the editing property on the table view, and then return a different cell type depending on which state the table is in.