Reputation: 371
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.
Upvotes: 31
Views: 70162
Reputation: 5893
Objective C
NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];
Swift code
let indexPathForSelectedRow = self.tableView.indexPathForSelectedRow
Upvotes: 109
Reputation: 2104
in UITableViewDataSource delegate there is a method - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath.
It returns NSIndexPath object which caontains both the selected section and selected row.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"Selected section>> %d",indexPath.section);
NSLog(@"Selected row of section >> %d",indexPath.row);
}
make sure to set datasource of tableview before using it otherwise this method won't be called
Upvotes: 6
Reputation: 31
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int a = indexPath.row;
NSLog(@"index: %i",a);
}
Upvotes: 2
Reputation: 155
Here's the built-in method that will help you determine which cell has been selected.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// you can use "indexPath" to know what cell has been selected as the following
NSLog(@"Selected row is %@", indexPath);
}
and by the way the method is already given when you create TableViewController, you probably just have to uncomment it.
Hope you find it helpful
Upvotes: 1
Reputation: 47119
Use this delegate method of UITableView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%d", indexPath.row); // you can see selected row number in your console;
}
Upvotes: 16
Reputation: 213
I think what you are trying to do is not to get the index of the table view cell clicked but to know which cell is clicked.
To know this at the time of assigning each cell assign the indexpath.row value as the tag of the cell created then on clicked take the table view as the parent and try to get its subview(cell) with that tag
[Tableview view with tag:indexpath.row]
Upvotes: 1