UITableView scrolling to last cell only displays half of the cell - iphone

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.

Related

Proper way to implement a footer in UITableView

So I read here that I can simply drag a UITableViewCell to the bottom of my UITableViewController in storyboard and have it act like a footer. This footer row has an activity indicator in it that's it. It's width is 320 and height 55 with the indicator centered in the row. Note that it's a UIView rather than a UITableViewCell because I can only get it to work with the former.
First, The UITableView doesn't stop at the footer. One can see the footer if he extends his scrolling beyond the bottom of the UITableView. As soon as the user releases his finger, the footer disappears from site as the UITableView returns its scrolling back to the last element. I am trying to do what Instagram is doing - if you scroll to the bottom you can see their custom activity indicator at the bottom and the UITable will remain its scrolling position at that indicator. How can I achieve this?
Second, I have some custom scrolling performed by certain user actions. Right now, I have the following code:
if (row + 1 < [self.tableView numberOfRowsInSection:0]) {
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:row+1 inSection:0]
atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
} else {
// TODO scroll a little down so the user can see the activityIndicator
}
How can I tell my tableView to scroll programmatically to the footer and have it stop there?
Third During the very beginning, my UITableView has to fetch things from my server before it can populate the tableView. Unfortunately i can't get the footer view to maximize the space of the UITableView so that the activityIndicator will appear in the center. Right now this is how it looks:
I think I have the structs set correctly:
I suspect that having a UIView within a UITableView might prevent the view from maximizing.
Summary
Any recommendations on the above issues? I apologize for the length of this question, but I feel that they are all related to the same problem mentioned above.
I finally figured it out.
With regards to #zing and #Lithu answers, that worked. However, returning a footerView from this method forces the footerView to "stick" to the bottom of the TableView. By this I mean that the footerview will always be shown (which is not something I want)
I finally used self.tableView.footerView = MyView. I disabled ALL the spring and struct settings on my UIView and it fits perfectly. (I've previously set it to maximize hoping to have it maximize when there is nothing in the table)
Regarding centering the activity indicator: I ended up setting the footer view to hidden if there's nothing in the UITableView (by checking in numberOfRows). I programmatically added the ActivityIndicator in the center if this occurs.
Thanks for all the answers!!
Did you try to do it in the proper delegate for UITableview Footer
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
Use This code to add any view in the footer.
e.g.
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.tblView.frame.size.width, 49)];
return footerView;
}

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.

Added view in UITableViewCell getting cleared after scrolling

I am adding horizontal scrollview with images in UITableView cell on selection .On selection cell expanded to show scroll view . When i scrolled up or down added scrollView getting cleared .How to persist table view contain on scrolling?
set dequeueReusableCellWithIdentifier to nil like bellow
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
problem might be that you are reusing the cells via dequeueReusableCellWithIdentifier, if this is the case make sure you do not add scroll view every time cell loaded in to memory. Moreover if you add some code snippiest it would help to understand the actual problem

how to center uitableview selection with indexpath value

I am saving the indexPath selection from a subview and passing it to the parent view with delegates. When I got back to my subview from my main view I pass the indexPath back to it and show the user which cell they previously selected with a tick in the accessory view of the tableviewcell.
One problem being if the user has selected a cell out of a fairly big list its hard to find the cell they selected again incase they wanted to change it (being that they made a mistake)
I would like to know if their is a way to use indexPath or something similar to center the previously selected cell of the uitableview to the center of the screen?
UPDATE::
Here is a graphical view of what I am trying to achive to make it abit more understandable..
step one : select cell then go to subview and select the cell (value) you want to pass back to main view (save indexPath of selected cell)
step two: user either wants to change his selection or made a mistake and was ment to select the cell below the one they chose... repeat previous steps but display previously selected cell in the center of the view..
Have you tried the following function
[tableView scrollToRowAtIndexPath: atScrollPosition:UITableViewScrollPositionMiddle animated:YES]
a UITableView is a subclass of UIScrollView - try setting the Content Offset (figure out how much with the cell height and indexPath).
[tableView setContentOffset:CGPointMake(0, indexPath.row*cellHeight) animated:YES];
should work. You might want to do the math a little differently.

iPhone - having selected cell move to top of uitableview

I looked for this problem and I don't believe I could find an answer. I have a tableview of custom cells that when clicked, the selected cell pushes a new cell with information. I was wondering if anyone had an idea of how to push the selected cell to the top of the uitableview, or make it fill up the entire tableview.
My uitableview only takes up half of the screen, and I wish for when the cell is selected that it only take up that half of the screen, and the user is still able to scroll the cell (if that makes sense)?
Any suggestions would be appreciated, it was somewhat difficult to describe what I am looking for, so if anyone needs clarification please do not hesitate to ask me.
in cellForRowAtIndexPath, you need to place this in after you have created your cell
[tableView scrollToRowAtIndexPath:selectedCellIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
This did the trick for me with the custom height of the cells as well.
In my app I have a bunch of cells that expand to be larger when clicked; and only 1 cell can be expanded at a time. I basically keep track of the current selected indexPath, and then in heightForRowAtIndexPath: check to see if this is the selected sell; if it is I return a larger height.
Then, in didSelectRowAtIndexPath: I just set the current indexPath, and reload both the new cell and the previous one. This sounds similar to what you are looking for... would that work?
To move the selected cell to the top of the tableview, you could store the selected cell's index in an instance variable in didSelectRowAtIndexPath, and then call [tableView reloadData].
In your datasource's tableView:cellForRowAtIndexPath: method, you would ensure that the selected cell is returned for index 0 and all cells prior to the selected cell are returned for their index + 1.
If you are looking to move the cell, this is a base you could start from.
-(void)moveIndexPathToTop:(NSIndexPath *)pathToMove
{
[myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:pathToMove] withRowAnimation:UITableViewRowAnimationFade];
NSIndexPath *firstRow = [NSIndexPath indexPathForRow:0 inSection:0];
[myTable insertRowsAtIndexPaths:[NSArray arrayWithObject:firstRow] withRowAnimation:UITableViewRowAnimationFade];
}
This will move the row as far as the UITableView is conserned. Of course you'll need to match the new order in the data source also. (Like changing the order in your array of data)
Also, you'll probably want to scroll the table to the top when you do this.