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.
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.
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.
Objective C
NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];
Swift code
let indexPathForSelectedRow = self.tableView.indexPathForSelectedRow
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With