Iphone 单击单选按钮时
在我的应用程序中,我在表视图的每一行中实现单选按钮。 问题是当我单击第一行的单选按钮时,在某个地方,即第五行的单选按钮被选中 我需要选择我正在单击的按钮。这是我所做的代码 提前谢谢Iphone 单击单选按钮时,iphone,radio-button,Iphone,Radio Button,在我的应用程序中,我在表视图的每一行中实现单选按钮。 问题是当我单击第一行的单选按钮时,在某个地方,即第五行的单选按钮被选中 我需要选择我正在单击的按钮。这是我所做的代码 提前谢谢 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITab
(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);
}
这段代码有几个问题 1) 您没有处理[tableView dequeueReusableCellWithIdentifier]实际返回要使用的有效单元格的情况,相反,您只在“cell==NULL”时才进行工作 我打赌,如果设置断点并查看(或NSLog out)indexPath.row的值,您将看到“cell==NULL”案例中的代码只会偶尔被调用 2)
“btn”的用途是什么?您可以分配它,然后不使用它执行任何操作。此代码有一些问题 1) 您没有处理[tableView dequeueReusableCellWithIdentifier]实际返回要使用的有效单元格的情况,相反,您只在“cell==NULL”时才进行工作 我打赌,如果设置断点并查看(或NSLog out)indexPath.row的值,您将看到“cell==NULL”案例中的代码只会偶尔被调用 2) “btn”的用途是什么?你分配它,然后什么都不做