Reputation: 21
In my application I am implementing radio buttons in each row in table view. The problem is when I am clicking on 1st row's radio button, somewhere i.e, 5th row's radio button was selected.
I need to select the button which I am clicking. here is the code what I did.
Thanks in advance.
(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] autorelease];
UIImageView *backGroundImageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"History_strip_bg1.png"]];
[cell.contentView addSubview:backGroundImageView];
[backGroundImageView release];
cell.accessoryView=[[ UIImageView alloc ]initWithImage:[UIImage imageNamed:@"arrow2.png"]];
if(doneBtnFlag)
{
radioBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[radioBtn setImage:[UIImage imageNamed:@"radio_button_1.png"] forState:UIControlStateNormal];
[radioBtn setImage:[UIImage imageNamed:@"radio_button_selected.png"] forState:UIControlStateSelected];
[radioBtn setFrame:CGRectMake(2, 17, 25, 25)];
[cell.contentView addSubview:radioBtn];
[radioBtn addTarget:self action:@selector(radioButtonClicked:event:) forControlEvents:UIControlEventTouchUpInside];
radioBtn.tag = 1;
}
btn=(UIButton*)[cell.contentView viewWithTag:1];
}
return cell;
}
- (IBAction)radioButtonClicked:(id)sender event : (id)event
{
btn.selected = YES;
rightBtn.enabled =YES;
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:contactsTblView];
NSIndexPath *indexPath = [contactsTblView indexPathForRowAtPoint: currentTouchPosition];
NSLog(@"indexPath %@",indexPath);
}
Upvotes: 1
Views: 398
Reputation: 89509
This code has a few problems.
You are not handling the case where [tableView dequeueReusableCellWithIdentifier] is actually returning a valid cell to use, and instead you're only doing work if "cell == NULL".
I bet if you set breakpoints and look at (or NSLog out the value of) indexPath.row, you'll see the code inside the "cell == NULL" case only gets called sporadically.
What is "btn" used for? you assign it and then don't do anything with it.
Upvotes: 1