How to get UITableviewcell value on click on button? - ios

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;

Related

SKSTableView Got wrong Indexpath when clicked Button inside SubRowAtIndexPath

I am using SKSTableView control.I have added button on cell.I have facing one issue When i clicked on button then i got wrong row and subrow.
- (IBAction)btnPickListPressed:(id)sender
{
UIButton *btn=sender;
PicklistTableViewCell *cell = (PicklistTableViewCell *)btn.superview.superview;
NSIndexPath *indexPath = [self.tblView indexPathForCell:cell];
NSLog("%#",indexPath.row);
NSLog("%#"indexPath.subRow);
}
First copy below code and open SKSTableView.h
- (NSIndexPath *)correspondingIndexPathForRowAtIndexPath:(NSIndexPath *)indexPath
paste this below to #interface SKSTableView : UITableView in SKSTableView.h
and replace your action method code to this:
UIButton *btn=sender;
PicklistTableViewCell *cell = (PicklistTableViewCell *)btn.superview.superview;
NSIndexPath *indexPath = [self.tblView indexPathForCell:cell];
NSIndexPath *correspondingIndexPath = [self.tblView correspondingIndexPathForRowAtIndexPath:indexPath];
NSLog("%#",correspondingIndexPath.row);
NSLog("%#"correspondingIndexPath.subRow);

How can I get a uitableViewCell by indexPath.row or only row number instead of indexpath?

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];

Trying to change cell content doesn't work

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... :-)

how can i get the section number of a selected custom TableViewCell?

how can i find out in which section i clicked? i need the number to send the item to another view:
- (IBAction)mapButton:(id)sender {
UIButton * myButton = sender;
int row=myButton.tag;
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:row inSection:0];
MapViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:#"mapView"];
self.selectedStandort = [self.fetchedResultsController objectAtIndexPath:indexPath];
detail.currentStandort = self.selectedStandort;
[self.navigationController pushViewController:detail animated:YES];
}
Is the mapButton a subview of the UITableViewCell?
Then I would do the following:
NSView *contentView = [sender superview];
NSTableViewCell *cell = (UITableViewCell *)[contentView superview];
NSIndexPath *cellIndexPath = [myTableView indexPathForCell:cell];
NSInteger section = cellIndexPath.section;
You can also use the row from the cellIndexPath, so you don't have to keep your tag value up to date (e.g. on reuse, reorder and delete) which makes your implementation less error-prone.

Get Selected index of UITableView

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.

Resources