I want to have selected index for UITableView.
I have written following code:
NSIndexPath *index = [NSIndexPath indexPathForRow:1 inSection:0];
[tableView scrollToRowAtIndexPath:index
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
This always work for 1st item. I want to have selected index in indexPathForRow.
Please help.
Thanks.
NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];
Get current selected row.
May be helpful if you have only one section.
- (NSUInteger)currentSellectedRowIndex
{
NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
if (selectedIndexPath) {
return selectedIndexPath.row;
}
else {
return NSNotFound;
}
}
Use this code
CGPoint location =[sender locationInView:self.tableView];
NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell *swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath];
NSIndexPath *indexPath = [self.tableView indexPathForCell:swipedCell];
In didSelectRowAtIndexPath, set an interface declared NSIndexPath as the indexpath returned. Then you can scroll to that variable. If you need help, comment and I can give some sample code.
Swift 4.2
let selectedIndexPath = tableView.indexPathForSelectedRow
which is an optional return value.
Related
I want a specific section indexPath but this code only give me section 0 indexPath because InSection I set 0.
if there is multiple section then how i get indexPath?
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:0];
help me with that.
If you click on button the below code is useful
-(IBAction)btnClicked:(id)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:tableviewName];
NSIndexPath *indexPath = [self.accountTablView indexPathForRowAtPoint:buttonPosition];
NSLog(#"section index-----%ld",(long)indexPath.section);
}
The tag attribute on sender is returning it's default value.
Ensure you set the tag appropriately during senders creation, it should be set as:
myView.tag = indexPath
Now, the sender will return the correct indexPath values while doing:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:0];
Just ensure tag is set correctly (I'm guessing in cellForRowAtIndexpath:) to retrieve the correct indexes.
I need to access the title of a selected row of a Table View.
But the issue arises when i need to access it outside the TableViewDelegate, i.e. : cellForRowAtIndexPath, didSelectRowAtIndexPath, etc.
Is there anyone who could help me.
Thanks in advance.
How about this ?
- (IBAction)ButtonAction:(id)sender {
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:tableView];
NSIndexPath *selectedindex = [tableView indexPathForRowAtPoint:buttonPosition];
UITableViewCell *Cell = [tableView cellForRowAtIndexPath:selectedIndexPath];
}
I want to get UITableviewCell value on click of update button. When user press on update button then i want to fetch first cell(name) and second cell(oil cost) value. Please help me.
:(
Try below code,
NSIndexPath *indexPath = [NSIndexPath indexPathForRow: 0 inSection: 0];
UITableViewCell *cell1 = [self.tableview cellForRowAtIndexPath:indexPath];
NSString* name = cell1.textLabel.text;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow: 1 inSection: 0];
UITableViewCell *cell2 = [self.tableview cellForRowAtIndexPath:indexPath];
NSString* oilcost = cell2.textLabel.text;
we can get the UITableViewCell from indexPath easily like this:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
But i only have the row number not the index path, i am using this code to get the cell .
NSArray *visible = [self.tableView indexPathsForVisibleRows];
NSIndexPath *indexpath = (NSIndexPath*)[visible objectAtIndex:MyRowNumber];
UITableViewCell *tablecell = [self.tableView cellForRowAtIndexPath:indexpath];
Tell me whether its a good approarch and if not which one is.
You can create a new NSIndexPath with the row number (assuming your table has only one section).
NSIndexPath *indexPath= [NSIndexPath indexPathForRow:yourRowNumber inSection:0];
I have a UISearchController and i want to select the first row of the searchResultsController tableview on click of search button in the keyboard.
For that I tried implementing
-(void)searchButtonClicked:(UISerachBar*)searchBar {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.searchResultsController.tableView selectRowAtIndexPath: indexPath animated:false scrollPostion:UITableViewScrollPositionNone];
[self.searchResultsController tableview:self.searchResultsController.tableView didSelectRowAtIndexPath: indexPath];
}
but it didn't work for me!!
Then I tried to make a separate method:
-(void) plotPinOnMapWithIndexPath:(NSIndexPath*)indexpath;
and called it in didSelectRowAtIndexPath and in searchButtonClicked method by passing the indexPaths.But the problem is that the cell doesn't get highlighted the way it gets highlighted when user clicks on it.
This works for me, and is correct:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
optionally try this:
[self.tableView:self.tableView didSelectRowAtIndexPath:indexPath];
or you can use Segue to continue with result:
[self performSegueWithIdentifier:"showDetailView" sender:self];
In prepareForSegue method you pass parameters....
It worked!!
-(void)searchButtonClicked:(UISerachBar*)searchBar {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self tableview:self.searchResultsController.tableView didSelectRowAtIndexPath: indexPath];
}