Expanding UITableViewCell on Tap? - iphone

I need to expand a UITableView cell when I tap it (animate it as well). However I don't know the cell which is being tapped and I have seen the answer here: Can you animate a height change on a UITableViewCell when selected?
It seems to be a very popular answer, not just in the question. However, I need to get the cell, so how in the heightForRowAtIndexPath method can I get the cell?
Because I have tried this:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
However using this in the heightForRowAtIndexPath method crashes the app as the first time it runs (when the view loads) it gets caught in an infinite loop I think.
Anyway if I was to take this approach how would you suggest i get around this issue? I need to access the cell. Or how else would you expand your tableview cell (animated)?

Carry on adjusting the height as you are.
I would suggest trying to get as much as you can from autoresizing masks as they have worked well for me in the past.
If you need finer grained control you can create a subclass and override the setFrame: method to find out when the frame is changing size and then set up animations appropriately there.

Related

How can I replace `UITableViewCell` with a `UIScrollView` or `UITableView` after the main `UITableView` is already loaded?

So I am messing around with how this feels in my app. I have a UITableView and I have the swiping/panning in place, where I kind of flick the cell away and once the cell is away I have it call a method in the same class where I have my UITableView as a property. Once that cell is swiped away and that method gets called, I want to animate the size of the now empty cell to be about 3 times the size of the cell and have a UIScrollView or UITableView be added. It will be a place to store all comments about that cell.
Basically, I would just like some ideas on how I can replace a swiped cell with a UIScrollView or UITableView and animate it to three times the size of any cell.
In a nutshell, if the text below is tl;dr:
call reloadRowsAtIndexPaths, remember which cell you've changed, update cellForRowAtIndexPath and heightForRowAtIndexPath accordingly, and you are set to go.
Full version:
When you said swiped away, did you meant that you are reloading or deleting certain row of your UITableView?
If so, I think you might be able to do what you described by calling [tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft]; (or whatever direction you'd like), then saving the indexPaths you've changed (in your case only one indexPath), and react appropriately in the cellForRowAtIndexPath dataSource method - changing the normal cell for another one, custom-made, with UITableView or UIScrollView inside, and also reacting in the heightForRowAtIndexPath delegate method, to change the height.
The animation itself will be handled by tableView, so that's an ease for you.
Also, if you will use this method to change the contents of the cell for UITableView, don't forget to assign delegate and dataSource of that cell's new tableView, since that causes lot of troubles when forgot.

How can I set the UITableViewCellAccessory for a chosen row? (iPhone/iPad)

I need a way of setting the UITableViewCellAccessory for any row. However the catch is that I need to be able to do it OUTSIDE of the UITableView delegate methods.
I have tried this, but it doesn't show up the accessory.
[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathWithIndex:1]] setAccessoryType:UITableViewCellAccessoryCheckmark];
If it makes any difference I created the UITableView in IB in a storyboard. The data is static, and I'm using a grouped table style with only one section.
Please can someone help me out?
If your table view is scrolled so the cell in question may sometimes scroll out of view and then back into view, you should manage the content of that cell only from within the UITableView method cellForRowAtIndexPath:. The reason is that when cells are redrawn, the tableview object calls this method to make sure that visible cells are properly rendered. (Cell that are not visible don't need to be rendered at all.)
That being said, this is where you should handle the cell content, even if the table view doesn't scroll the cell in question out of view. It wil lmake you life a lot easier if you follow this design pattern when working with table views.
Inside that method, you can test (using if statement, for example) the value of the indexPath.section and indexPath.row so that you can configure the specific cell the way you want it. This includes putting in the accessory.
Always use [NSIndexPath indexPathForRow:inSection:] when working with table views.
You can figure out what indexPath you need the checkmark on and then use something like this
UITableViewCell * cell = [tableView cellForRowAtIndexPath:someIndexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;

Animated change of uitableviewcell height and toggle of contents

I have a tableview with UiTableViewCells from a XIB and I want to animate a change of cell height when selected and showing/hiding some elements.
Now, for the animated change of height there's no problems, I've done it with the tutorial in this answer .
The problem is how to toggle some elements!
I can't figure out how to solve this problem! Putting on the xib the elements, they cover the cell above; by code I can't find a method to call when a cell is opened and the animation i finished!
Any ideas?
Thanks to all!
There may be an easier way than this, but one way would be instead of changing the cell height and adding the new components in the animation block, set a flag for that row that it should be expanded then in your animation block call
[tableview reloadData]
And in your
heightForRowAtIndexPath
method return the new cell height and in the
cellForRowAtIndexPath
method of your delegate return the new version of the cell with the additional elements.

Weird UITableViewCell bug: accessory views change at random?

So, I have a UITableView set up to display all the system languages, where the user can select one. Upon selection, the cell sets its accessory view to a check mark and sets all the other cells' accessory views to none. But for some reason, when one cell is selected, another cell will randomly get selected as well.
For example: Here I select English UK.
But then when I scroll down (here showing the bottom of the list), English US seems to have automatically selected itself.
And then when I scroll back up to the top, English UK has magically deselected itself, but Spanish has been checked somehow.
Here is the entire source of the ViewController subclass: http://pastebin.com/EYNS9ahk
I also tried implementing a delegate method to check if the tableview is inadvertently selected at any point, but that's not the problem. Any ideas as to what is going on here?
You code seem me fine and well written, I guess the issue could be with the cell reusability,
Try with do the below changes,
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell_%d",index.row];
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
let me know if you are still getting the same issue.
Since the cell is reusable, when scrolling, the cells outside the view port will enqueue, and the -tableView:cellForRowAtIndexPath: will dequeue the enqueued cells for reusing, the cells' property remain the status on enqueuing, so you need to set to new status.
to fix your problem, you need to remember the selected cell's indexPath or other things can identity the cell, and set the accessoryType for it in -tableView:cellForRowAtIndexPath:, do not rely on the -tableView:didSelectRowAtIndexPath:.

Decrease UITableViewCell width and put custom button on the left

Is there any way to decrease the standard width of grouped UITableViewCell and put a custom button on the left side(outside of cell boundary)? I tried to change the cell size but it keeps same
You are going to have to fake the editing mode.
What I mean by that is that as AtomRiot said you have to subclass UITableViewCell so that when in editing mode you show the button you want on the left, outside the cell.
But first things first.
To change the indentation level for your cells all you need to do is implement this delegate method for the UITableView
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
So that takes care of it. Then in your UITableViewCell subclass all I would do is to implement the method
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
which I assume is called when the table the cell belongs to has changed to editing mode.
There I would fade in (or animate in any way you want) a button to appear on the left of your cell.
I have done it inside a grouped-style cell but never on the outside.
Give it a try!
You could subclass UITableCell and add your own custom views inside of it. I have not personally added a button inside one but it should work. It may get confused with the row selected call the tableview makes if you are implementing that.
The Cocoanetics blog seems to have a pretty good solution to this:
http://www.cocoanetics.com/2010/03/how-to-shrink-cells/