I have array of 20 elements, and table view that can show only 5 of them. On start of view, I am selecting 1 item, and I want to show that item in table view by selecting it. When I choose one of first 5, selection is visible. When I select 5+ element, I want that element to be visible too by moving content offset. How can I scroll TableView to specific element from list inside tableview array? In which function of table view delegate do I do that?
I tried setting content offset, and scrollToRowAtIndexPath inside cellForRowAtIndexPath method, but it is not working correctly. Any ideas?
Calling scrollToRowAtIndexPath: at cellForRow will not work..First you need to reload data of tableview and then you need to call as below after selecting an item..
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:selectedRow inSection:0];
[self.tableView reloadData];
[self.tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop animated:YES];
Hope it helps you..
Put your scrollToRowAtIndexPath inside ViewDidLoad or ViewWillAppear of the TableViewController.
Related
I have a UITableView in that I am loading photos with comments..
I have horizontal scrolling of images in each row and vertical scrolling of users photos horizontally with comments..
I want to update the single cell when ever the user commented the photo.
At scrollviewDidScroll method I have code for dynamically updated the height of UITableviewCell and displaying the comments. As my requirement is dynamically increase height of custom cell with respect to comments of the photo scrolled. I am displaying latest three comments in the cell. It should dynamically updated the height based on 0 or 1 or 2 or 3 comments of that photo..
So, I have tried with below code..For updating the single row..
[self.photosTable beginUpdates];
[self.photosTable reloadRowsAtIndexPaths:[self.photosTable indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.photosTable endUpdates];
[self scrollViewDidScroll:self.photosTable];//As UITableView is sub class of UIScrollview I wrote like that
But scrollviewDidScroll is not calling. If I scroll the table then only it is calling..
Please suggest the ideas where I went wrong..
Thanks in Advance..
scrollviewDidScroll is a delegate method which is automatically called when the UITableView is scrolled.
If you want to scroll the UITableView, try and use this code :
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:n inSection:0];
[self.tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
As others have pointed out, you should not call your own delegate methods.
This seems like an XY problem.
Take a step back and explain what you are trying to accomplish here. Why do you need your scrollviewDidScroll method to be called?
I have a program that selects rows at different times which works perfectly fine. But when i try to scroll down and try to select another row "out of view" while a row has already been selected by my program some where, it doesn't allow me to select that row which i clicked on. It only works if the my selected row is in the current displayed cells or display view. I have enabled multiple selection but still doesn't work.
-I've also find out that didSelectRowAtIndex isn't get called when selecting row outside the view where the program has already selected a row
I have found a solution. didSelectRowAtIndex didn't get called because my scroll position of tableView was set to UITableViewScrollPositionTop and this wasn't allowing me to select another row outside the view. All i did was to change it to UITableViewScrollPositionNone And adding scrollToRowAtIndexPathwith scrolling position to none will allow minimum scroll with selected row visible at bottom.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:*rows inSection:0]; // set to whatever you want to be selected first
[tableview selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
[tableview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:NO];
}
I have custom UITableViewCell which has button that when clicked reveals a checked box.
Now I am creating this in xib and using a tableView to load it.
My problem is I need to set the selection state of the cell to true.
But when I do that reused cells lose selection property.
Is there any way to force the tableView to accept the cells selection?
Try this:
NSIndexPath *idxPath= [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:idxPath animated:YES scrollPosition:UITableViewScrollPositionNone];
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
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];