UITableView reloadSections creates a sectionheader on top of a row - iphone

I have a tableview with one sectionheader. With the following codesnippet I reload the tableview when a user swipes the view left or right.
[self.tableView beginUpdates];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];
When a user has scolled downwards in the tableview and then swipes, this codesnippet reloads the rows correctly but the sectionheader is created on top of the row where the swipe was initiated.
Can anyone tell me what I'm doing wrong & how to solve ?
thanks
Frank

I think the problem is that reloadSections:withRowAnimation seems to be buggy on iOS 4.0, and it was fixed in one of the later versions.
On iOS 4.0 calling reloadSections:withRowAnimation results in table header appearing in the middle of a row, and on 4.3 it works fine. I've replaced it with reloadData which is less optimal, but works on 4.0 too.

Related

UITableView won't reload contentSize

I have a UITableView, that add's some data after the UITableView reloaded for the first time, but I can't scroll down to the new data if I scroll I see the new data but then it scrolls back automatically, and yes I reloaded my UITableView, so I checked my UITableView contentSize and it won't change after the reload but after I go to another view (UINavigationController) and pop back the UITableView contentSize does change and it works! How can I fix this?
Thanks!
I had a similar issue and solved it using the same technique as Daan (as far as I can tell).
In my case I was using a static UITableView that had some of its rows and sections hidden initially (by setting appropriate return values from numberOfSectionsInTableView: and tableView:numberOfRowsInSection:). I was pushing a VC onto the nav stack to collect additional data which I would then use to fill in the hidden tableview sections, calling reloadData on the tableview to refresh it.
This worked fine in iOS7, but in iOS6 the tableview's contentSize.height never changed from its initial value (218 pts) to the taller value (504 pts) derived from the addition of the new sections. As such, you could not scroll to the content at the bottom of the tableview. Trying to force the contentSize did not work as it was immediately set back to 218.
Changing the tableView's contentOffset allowed me to scroll to the bottom of the content, but if you tapped the UITextField in the last cell, it would whip off screen as the keyboard was shown.
I finally was able to come up with a solution that worked in both iOS6 and iOS7 without issue. I used the old beginUpdates / insert or delete rows and sections / endUpdates methods of UITableView, as follows:
// change the tableView's data source to reflect insertions/deletions
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:#[ [NSIndexPath indexPathForRow:1 inSection:0] ] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView insertSections:[NSIndexSet indexSetWithIndexesInRange:NSRangeFromString(#"1,3")] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
As you can see, in this particular case I had to remove a row from the first (and only) section and add three sections to the end of the tableview.

UITableView scrolling to last cell only displays half of the cell

I want to scroll to the bottom of my tableview that contains custom cells.
Here is the code I am using to scroll:
NSIndexPath *lastMessage = [NSIndexPath indexPathForRow:[self.conversation.messages count]-1 inSection:0];
[self.messageTable scrollToRowAtIndexPath:lastMessage atScrollPosition:UITableViewScrollPositionTop animated:YES];
This scrolls the view; however only the very top of the last cell is visible with ~3/4 of the cell still below the fold that I have to manually scroll down to. Any ideas on how to fix this?
Turned out to be a timing issue. The tableview hadn't fully rendered yet when I called that method from viewdidload (contentsize of 0). Calling this method in viewDidAppear works brilliantly though.
It seems like the UITableView is confused about how big your cells are. Set the rowHeight property on the UITableView to the height of your custom cell.

adding subviews to UITableViewCell issue

I have a UITableView , in which if I tap on one of the rows it adds a subview at the bottom. The issue is that when I tap the last row in the table, it hides the subviews and having me to scroll to the bottom to see it. It's a small bug, but what is the best way to remedy this issue.
One way I can think of is to scroll down to the bottom of the row if the last row is selected.
This doesn't seem to be a very good solution though.
Here's a video illustrating my issue
You may want to try something like this in the didSelectRow method:
if (indexPath.row == [dataArray count]) {
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row
inSection:0]
atScrollPosition:UITableViewScrollPositionBottom
animated:YES];
}
I've also used the following for making the last row visible (though from your video I do not think this will work for your situation)
[guessesTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];

How to change uitableviewcell's height after tableView.reloadData

I am working on a twitter-like client, and there is a message list view implemented by UITableView. For each cell, it may need to load a dynamic size image, and the image is loaded async. So it size of the image is unknown when the table view loaded.
Is it possible to update the uitableviewcell's height after the image is loaded.
From my knowledge, the only way to update the height is in this method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
But it is just called when loading data at first time. This is reasonable from performance consideration. Anyhow, is there a way to change the height later?
Thanks for any information!
Finally, I used "reloadRowsAtIndexPaths" to update particular rows, you can try this for hidden cells.
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:toReloadRows withRowAnimation: UITableViewRowAnimationNone];
[self.tableView endUpdates];
But, the cell shakes when updating the visible cells's height. So the it's better to have the cell height calculated before.
Try calling an empty Update block:
[self.tableView beginUpdates];
[self.tableView endUpdates];
This seems to work for visible cells. I'm still struggling with hidden cells.
heightForRowAtIndexPath: is actually called again when you perform a reloadData (you could set a breakpoint to verify). All you need to do is put the conditional logic you need in there.
[tableView reloadData];
will do what u want. It will reload entire table datasource. So u can set rowHeightaccordingly

Want some animation in custom UITableView edit mode

I have very custom TableView like Add Contact View in iPhone's Contacts.
I like the way this view arrange rows in animation when I click Edit button.
My table view has for example 2 rows of editable information and 6 rows non-editable.
I wrote some code and this 6 non-editable rows disappear from screen when user click my custom Edit button. But its very "flat" and not interesting. Its just remove rows and reloadData then;
I want to add some nice animation in process of removing cell and appearing back after "Done" click.
I can't use standard "setEditing animated" because my view custom and I don't need insert or delete rows - i just edit information in it;
Thanks :)
oh i think i found it in documantation )
[tv beginUpdates];
[tv insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];
[tv deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[tv endUpdates];