Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS, How to find selected row Of UITableView?

I'm trying to find selected UITableViewCell as following:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {      static NSString *CellIdentifier = @"Cell";      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];      if (cell == nil) {          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault          reuseIdentifier:CellIdentifier];    }     if   ([indexPath row] == [tableView cellForRowAtIndexPath:indexPath.row]) // i want to place value of selected row to populate the buttons           //([indexPath row] == 0)          //(indexPath.row == ![self cellIsSelected:indexPath])       {         UIButton *AddComment = [UIButton buttonWithType:UIButtonTypeCustom]; // custom means transparent it takes shape same with backgroup image          [AddComment addTarget:self                         action:@selector(TestBtns:)              forControlEvents:UIControlEventTouchDown];          // [AddComment setTitle:@"1" forState:UIControlStateNormal];          AddComment.frame = CGRectMake(9.0, 128.0, 96.0, 26.0);          [cell.contentView addSubview:AddComment];          UIImage *buttonAddComment = [UIImage imageNamed:@"addcomment.png"];          [AddComment setBackgroundImage:buttonAddComment forState:UIControlStateNormal];          [cell.contentView addSubview:AddComment];        } 

How to achieve this, can you please guide me, where I'm doing Mistake.

like image 339
Pratap Manish Bhadoria Avatar asked Sep 05 '13 13:09

Pratap Manish Bhadoria


People also ask

How do you get the IndexPath row when a button in a cell is tapped?

add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.

How do I get a cell in Didselectrowat?

Use the below function and pass the index path of the selected row so that the particular cell is reloaded again. Another solution: store the selected row index and do a reload of tableview. Then in cellForRowAtIndexPath check for the selected row and change the accessory view of the cell.


1 Answers

Objective C

 NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow]; 

Swift code

let indexPathForSelectedRow = self.tableView.indexPathForSelectedRow 
like image 110
Suraj K Thomas Avatar answered Sep 20 '22 19:09

Suraj K Thomas