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.
Related
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 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];
}
I'm trying to change the background color of a cell after the user enters something in a textfield. I've tried the following code but nothing happens.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:2];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor grayColor];
Is there anything I don't see?
The only thing I can think of is that you have your section/row number wrong, i.e.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:2];
I have two sections in my tableView and it works as long as refer to section 2 as section 1, as section and row numbers start at 0. Apologies in advance if this sounds too obvious, but I do it all the time... :-)
There is UITableViewCell which contain UIButton.
In my cellForRowAtIndexPath:
cell.toCartBtn.tag = indexPath.row;
[cell.toCartBtn addTarget:self
action:#selector(toCart:) forControlEvents:UIControlEventTouchDown];
In toCart method I need to get row and section by tapped button tag. How can I do it?
See this code
- (void)toCart:(id)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
if (indexPath)
{
...
}
}
Use this simple code if UIButton is subview in cell
- (void)toCart:(id)sender
{
UIButton *senderButton = (UIButton *)sender;
UITableViewCell *cell=(UITableViewCell*)senderButton.superView;
NSIndexPath *index = [tableView indexPathForCell:cell];
}
Create a custom UITableViewCell subclass and handle the touch in there.
Then define a delegate for your custom table view cell and when you create the cell, assign your table view data source as the delegate.
When the cell handles the touch, send a message to the delegate and then find the index path from the table view.
-(void)customTableViewCellButtonPressed:(CustomTableViewCell *)cell {
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
...
}
try this:
- (void)toCart:(id)sender {
CGPoint button1 = [sender convertPoint:CGPointZero toView:self.tblName];
NSIndexPath *index = [self.tableView indexPathForRowAtPoint:button1];
//use index variable
}
and also see for more details for your bug...
Detecting which UIButton was pressed in a UITableView
For Swift
let buttonPosition:CGPoint = sender.convert(CGPoint.zero, to: self.tableViewItems)
let indexPath = self.tableViewItems.indexPathForRow(at: buttonPosition)
if (indexPath != nil)
{
print(indexPath?.section)
print(indexPath?.row)
}
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.