I am using this line
[self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
inside the cellForItemAtIndexPath method, to programatically select an item in the collection view and it is working, the problem is that it only works after I touch the collection view. How do I get it to work immediately after the collection view is loaded?
Call it inside viewDidLoad or viewDidAppear.
If you know the position you want to select on load, you can create a NSIndexPath object by using: [NSIndexPath indexPathForRow:inSection:]
For example: NSIndexPath *myIndex = [NSIndexPath indexPathForRow:0 inSection:0];
Related
I have an UITableView, if I move to the another screen and when I come back to the UITableView screen the scroll automatically stops at 1st index position, how can I overcome it.
Thanks.
Override viewWillAppear method like below and scroll to first IndexPath,
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tblList.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
}
For Objective-C,
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[tblList scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
For Objective c
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:self.sectionToShow];
[cont.tableview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
Your question and description are contradictory, either you want the tableview to remain at same position when user comes back to the page, or you want the tableview to scroll to top. If you go with first option then this solution could help you, for the second option you refer to PPL answer.
Save the tableview content offset before your push to another page.
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
tableViewContentOffset = self.tableView.contentOffset;
}
and set tableview offset again in viewWillAppear
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tableView setContentOffset:tableViewContentOffset];
}
The easiest solution is to finish your data fetching and calling of reloadData method of UITableView into viewDidLoad method of your UIViewController.
So whenever you move to another scene your previous screen will remain as it is, because viewDidLoad only called once when your UIViewController is initialized.
I've a tableView and I'd like to switch to another view by clicking a button in the center of the cell. I use dynamic cells.
So, I want to switch to a view (in a navigation controller) with the click of a button in a cell, how can I do it in prepareForSegue? I can get the index of the cell, and then to do this step by clicking the disclosure, but when I click on my button located in the center of the cell, the indexpath does not exist. Here the code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
UIViewController *destination = segue.destinationViewController;
if ([destination respondsToSelector:#selector(setDelegate:)]) {
[destination setValue:self forKey:#"delegate"];
}
if ([destination respondsToSelector:#selector(setSelection:)]) {
NSIndexPath *indexPath = [self.tableFeed indexPathForCell:sender];
NSString *idUser = [self.arrIDUser objectAtIndex:indexPath.row];
NSLog(#"%#",idUser);
NSString *pattern = [self.arrPattern objectAtIndex:indexPath.row];
NSMutableDictionary *selection = [[NSMutableDictionary alloc] initWithObjectsAndKeys:idUser,#"idUser", pattern, #"pattern", nil];
[destination setValue:selection forKey:#"selection"];
}
}
First you need to grab the cell, then its superview i.e UITableViewCellContentView, then the table that has this cell. Here is the code.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSLog(#"prepare for segue");
//sender is UIRoundRectButton
UITableViewCell *cell = (UITableViewCell*) [[sender superview] superview];
UITableView *table = (UITableView*)[[[sender superview] superview] superview];
NSIndexPath *indexPath = [table indexPathForCell:cell];
NSLog(#"%i %i",indexPath.section,indexPath.row);
}
This would give you the index path for the cell in which the button was tapped.
You can get the indexPath by going to the superview of the button to get the content view and then to the superview of the content view to get the cell. But this is kind of ugly.
A better approach can be found in iOS Recipes where they transform the origin of the button to the table view via convertPoint:toView: and then call indexPathForRowAtPoint: to get the indexPath.
you have to create button in cellForRowAtIndexPath delegate method and assign tag to taht button. Based on that you will get that which row has selected.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedDealIndex = indexPath.row; //here you will get selected index
[self performSegueWithIdentifier:#"your_seguename" sender:self]; //here you have to pass your seguename which you have written in xib[in properywindow]
}
I'm trying to pass on the label of a table row when you hit that rows accessory button as it matches the key of an array in a plist, so the next page knows which array to load the settings from.
This is the code I'm using at the moment in performSegueWithIdentifier:
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
SettingsViewController *destViewController = segue.destinationViewController;
destViewController.nameViewer = [viewerKeys objectAtIndex:indexPath.row];
Unfortunately every time it picks the first row... I know because using NSLog it always gives the same output (the name of the first row). Any Ideas?
Cheers,
Chris
The problem is that when you tap an accessory button of a cell, the cell itself doesn't get selected (this is normal behavior since accessory buttons fire a different delegate method), so you get always 0.
What you could do though is to set an ivar with the selected index inside tableView:accessoryButtonTappedForRowWithIndexPath: delegate method and pass that ivar to your destination controller. I hope that this makes sense...
Use -prepareForSegue:sender: instead of -prepareForSegue: and use the following:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSIndexPath *indexPath = [[[segue sourceViewController] tableView] indexPathForCell:sender];
SettingsViewController *destViewController = segue.destinationViewController;
destViewController.nameViewer = [viewerKeys objectAtIndex:indexPath.row];
}
I've often used prepareForSegue to pass stuff forward to the next view controller. No problem when my tableview doesn't use sections. But I've added sections to my latest project. Now I don't know how to use prepareForSegue because it doesn't receive the indexpath, which contains the section reference. What am I missing? Thanks for the help!
In your prepareForSegue method, just use:
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
and pass that along to the destinationViewController.
I have a splittView and on the left side of my splittview I added an UITable.
#interface LeftViewController : UITableViewController
I also added all the functions which are requiered for the my Tableview.
I also can see and put data into my tableview...
Now I was trying to add a background Image or set the first cell as a default cell and I notice that I have nowhere a tableview declared in my code ( except of the tableview functions)
How is that possible and how can I set now a background image or select the first cell?
I know that this is the code to select the cell, but there is no myTableView....
- (void)viewDidAppear:(BOOL)animated {
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
[myTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
}
Do I have to create a new tableview?
Best regards
Customizations are normally done in the viewDidLoad method. Since you are using a UITableViewController you can access the table view through the tableView property.
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];