Live resizing of UITableViewCell without using reloadData? - iphone

First of all, forgive me if the answer is already out there (I honestly have been working and searching for about two weeks).
And now, the situation:
I have a UITextView as a subview of a UITableViewCell in a UITableView. The UITextView is the first responder (while user is typing).
I have it set up (through delegate protocol methods) so that after each typed character, the cell's rowHeight is recalculated (correctly and in the tableViews delegate protocol for rowHeight). So far everything is fine.
Then, I want that cell to be reloaded (or refreshed, whatever the term may be) without resigning its subview, UITextView, as first responder.
A non-satisfactory solution around the problem:
I am able to use [tableView reloadData] and then [textView becomeFirstResponder] to simulate this (so that user doesn't even notice that the textView was reloaded) and the rowHeight is perfect, but this has been causing me a lot of problems (especially with making sure that the cursor does not jump to the end every time the user types a character and with autocorrect forcefully enforcing its suggestions due to textView resigning).
Is there a way to redraw or resize a UITableViewCell (to dynamically change its rowHeight) without causing any of its subviews to resign as first responder?

Using tableView's [tableView beginUpdates] and [tableView endUpdates] will solve this issue. See this answer for more information.

Related

UiTextView in Custom UiTableViewCell does not responds to didSelectRowAtIndexPath

I have a UITextView on a custom UITableViewCell and when I click on the UITextView, it does not trigger the didSelectRowAtIndexPath, how do I change this behavior so that that delegate is called as intended?
Ive tried putting the User enabled property of UITextView to false. When doing so it responds to the didSelect. But then Im not able to select the links that are mentioned in the TextView, which essentially is the main reason why i chose to go ahead with UITextView.
Any help or pointer guys
you can't do both the things at same time.
you can use UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath] in you didselectrowatindexpath method.
you need to give appropriate tag to every text field and by using [cell viewWithTag:<#(NSInteger)#>] you can get the text-field object and link which are mentioned in it .
If your table view is custom, you can fire didSelectRowAtIndexPath event yourself when the user touch the text view.
Well I managed to work around by putting a custom UIButton on UITableCellView and firing off the required methods on UIControlEventTouchUpInside. This ate my brains for 2 days!!

Expanding UITableViewCell on Tap?

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.

Trying to make a Custom UITableViewCell with a UITextField becomeFirstResponder when Editing

I have a UITableViewController with custom UITableViewCells that contain a UITextField. When switching the table view into edit mode, I add a new cell to the bottom of the table and would like to make this cell becomeFirstResponder. My tableView:cellForRowAtIndexPath: method checks for this bottom cell, so I just added the line:
[cell.theTextField becomeFirstResponder];
Which I believed should work. However, when the table view is first displayed, it does not seem to be working. BUT if I select the cell (making it the first responder), then go out of edit mode (causing a resignFirstResponder within my code), I can then go back into edit mode and magically it becomes the first responder as I would expect!
Note that even if I end editing mode with a different cell selected (they all have text fields) and go out of edit mode, then back in, it still works, as long as at some point I had made the last cell becomeFirstResponder (by selecting it).
So, my guess is that when it first becomes the first responder, there is something getting set either in the table view or some place else that wasn't originally set, and from then on it makes this work.
Anyone have any ideas as to what may be going on here?
The first becomeFirstResponder call fails because the cell doesn't have a superview yet. The table view adds the cell as its subview after you return it from tableView:cellForRowAtIndexPath:. I suggest you make this call somewhere else. If you already have a custom UITableViewCell subclass, you could implement didMoveToWindow: and call [self becomeFirstResponder] there.

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:.

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.