scrollToRowAtIndexPath with row 0 scrolls to header not row - ios

I have this line
[self.tableView scrollToRowAtIndexPath:self.cellIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
The cellIndexPath is section 2, row 0.
Instead of scrolling to the row it scrolls to the header of section 2.
Testing shows this seems to be the default behavior. Is there any way to override it and actually scroll to row 0?
Thanks

You could get the cell's rectangle and then scroll to that.
CGRect cellRect = [myTableView rectForRowAtIndexPath:cellIndexPath];
[myTableView setContentOffset:CGPointMake(0,(cellRect.origin.y)) animated:YES];

Related

Scroll past section header upon tapping section index?

I've implemented a UITableView that has section headers above each section. I've designed the table with the Grouped style so the headers scroll with the table instead of fixing to the top. I have also added a section index so users can tap/swipe to jump to a specific section. The problem is doing so jumps to that section but it shows the section header at the top. In this case that's undesirable - I want to scroll so the very top of the first cell is at the top of the screen, with that section header not visible.
How can you scroll just past the section header when the user taps an icon in the section index for UITableView?
Just try this
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 section:desiredSectionIndex];
[tableView scrollToRowAtIndexPath: indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
Found a solution. Perhaps there is a cleaner solution but this is working well for me.
Return -1 to prevent the table view from scrolling to any section when that method is called. Just before the return, add the logic to scroll to the desired location by getting and modifying the frame of the next cell.
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
CGRect nextRowRect = [tableView rectForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:index]];
nextRowRect.origin.y -= tableView.rowHeight;
nextRowRect.size.height += tableView.frame.size.height - tableView.contentInset.top - tableView.contentInset.bottom - tableView.rowHeight;
[tableView scrollRectToVisible:nextRowRect animated:NO];
return -1; //prevent auto scroll
}

uitableview doesn't scroll to bottom

I have a UITableView where I add rows dynamically. Basically a chat app.
I need to scroll the UITableView to bottom after reloadData
My code is:
It scrolls to here:
https://dl-web.dropbox.com/get/Screen%20Shot%202014-12-05%20at%2013.04.23.png?_subject_uid=44249794&w=AADgJkJJOGK_ANH_cXWnxeXmhFmF5KHjZllg-kP66HARdw
When manually scrolling, I can scroll even further to here: https://dl-web.dropbox.com/get/Screen%20Shot%202014-12-05%20at%2013.05.19.png?_subject_uid=44249794&w=AADtQEfMHMy0Ounri5W3gBTLNgR4uckT_gBdYQel9vD1qQ
My code:
[_chatTable reloadData];
NSIndexPath* indexPath = [NSIndexPath indexPathForRow: [[AppData getChatLog]count]-1 inSection: 0];
[_chatTable scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
I need the UITableView to scroll all the way to the bottom.
Sorry,I couldn't open up the given links.Try this
UITableView *yourTableView = yourTableViewToBeSetHere;
CGRect lastCellRect = CGRectMake(0, CGFLOAT_MAX, 50, 50);
// Scroll to the particular rect
[yourTableView scrollRectToVisible:lastCellRect
animated:YES];
Good luck!

How to show last add cell on UITableview whileloading view or reloading tableview?

all I am doing one chat application, i which i am using UITable view to display reply and response from user.in this case after some interval of time i am reloading my tableview to fetch new data from server. But the problem is that after adding new content to table view it will go at the bottom of table view and i have to scroll table view to see that one.or in other case whenever i am reloading my table it will show its first cell on view. Now my question is "is it possible to load last cell of UITableview after view gets load or reload table view?"
on search I find this line but it give me error
[sTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:sender.tag inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
what is sender.tag in this line? // use of undeclared identifier sender
This line working well but scrolling the page which i dont want
[table_readText scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES ];
Any idea or suggestion would be highly welcome.
Thanks in advance..
You can also look at the scroll view's setContentOffset:animated: method.
Going to the top would mean,
[self.tableView setContentOffset:CGPointZero animated:YES];
and the bottom would be,
CGFloat height = self.tableView.contentSize.height - self.tableView.bounds.size.height;
[self.tableView setContentOffset:CGPointMake(0, height) animated:YES];
SECOND option:
scrollToRowAtIndexPath:atScrollPosition:animated:
Scrolls the receiver until a row identified by index path is at a particular location on the screen.
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated

how to scroll tableview after inserting a row at the bottom correctly?

Here is the code I use:
//inserting a row at the bottom first
_numberOfRecords++;
[_tableView beginUpdates];
[_tableView insertRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:_numberOfRecords-1 inSection:0]] withRowAnimation:UITableViewRowAnimationBottom];
[_tableView endUpdates];
//clear text
_inputField.text = #"";
//then scroll to bottom
CGPoint bottomOffset = CGPointMake(0, _tableView.contentSize.height + 44.0 + _tableView.contentInset.top - _tableView.bounds.size.height);
NSLog(#"%f", _tableView.contentSize.height + 44.0 + _tableView.contentInset.top - _tableView.bounds.size.height);
[_tableView setContentOffset:bottomOffset animated:YES];
This would scroll the tableview in a very strange way.
But if I put the scrolling code BEFORE the insertion, it works fine except that it ignores the latest inserted row. That is, it scrolls to the second last row instead of scrolling to the very last row (of course, because it scrolls before inserting a new roll.)
So I believe this code has no problem of the position where it should scroll to.
The problem probably comes from row insertion to tableview.
It violates the animation of scrolling the table view.
I am doing this to make a chatting view.
Each time the user sends or receives a message, I insert a row containing the message to a table view, and scrolls it to the bottom. That's why I use tableView here. I tried to use scrollView with label, it works fine, but tableView seems more popular in a chatting view.
I was thinking to use scrollView or tableView, and I found the built-in message app of Apple is using a tableView, so I adopt tableView. Let me know if a scrollView with Label is better than a tableView.
Anyway, how can I scroll a tableView to the bottom after inserting a new row?
Try using UITableView's scrollToRowAtIndexPath::
[self.tableView scrollToRowAtIndexPath: atScrollPosition: animated:];
This is my own solution:
[_tableView reloadData];
//scroll to bottom
double y = _tableView.contentSize.height - _tableView.bounds.size.height;
CGPoint bottomOffset = CGPointMake(0, y);
NSLog(#"after = %f", y);
if (y > -_tableView.contentInset.top)
[_tableView setContentOffset:bottomOffset animated:YES];
Firstly reloadData after endUpdates. This ensures the tableView contentSize is updated after inserting a new row. Then check if the scrolling distance is greater than the contentInset.top (this is for avoiding the tableview hiding behind the status bar and navigation bar) then to scroll down, otherwise not to scroll because of some weird animation.
Alternatively, you can simply use
[self.tableView scrollToRowAtIndexPath: inSection: atScrollPosition: animated:];
to scroll to the row you want. But this doesn't handle cells with sections and footers very well. For plain tableViewCell, you can just use this to do the magic. Otherwise you may find my trick solution performs better.
Anyway, thanks for all your answers.

UITableView scrollToRowAtIndexPath doesn't scroll to the first row if it only has one item

I've implemented a tableView on the iPhone with the ability to search using the searchBar. I'm trying to mimic the effect that hides the searchBar in my tableView when it first loaded.
The behavior is expected when the tableView contains more than one row. However, it doesn't scroll to the first row when there is only one row. It shows the searchBar.
- (void)viewDidLoad {
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
atScrollPosition:UITableViewScrollPositionTop
animated:NO];
}
Actually I didn't understand all the things (may be something wrong, when you add cells to the table - where is search bar, in what cell?)
but you can try
[self.tableView scrollRectToVisible:CGRectMake(0, yValue, 10, 10) animated:YES];

Resources