2011-10-07 83 views
0

的功能,我可以选择 - 使用UITableView中的自定义UIButton取消选择特定的行。现在我想让整行启用点击选择。单击单元格我想给一般单选按钮

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath 
{ 

    static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 


if (cell == nil) 
{ 

    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    UIButton *rdoBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
    rdoBtn.frame=CGRectMake(7, 15, 28, 28); 
    [rdoBtn setBackgroundImage:[UIImage imageNamed:@"radiobut1.png"] forState:UIControlStateNormal]; 
    [rdoBtn setBackgroundImage:[UIImage imageNamed:@"radiobut2.png"] forState:UIControlStateSelected]; 
    rdoBtn.tag=2; 
    [rdoBtn addTarget:self action:@selector(radioBtnPressed:event:) forControlEvents:UIControlEventTouchUpInside]; 

    [cell.contentView addSubview:rdoBtn]; 
} 


UIButton *btn=(UIButton*)[cell.contentView viewWithTag:2]; 

if([appDelegate.countryName isEqualToString:[dict objectForKey:@"Country_Name"]]) 
{ 
    btn.selected = YES; 
} 
else 
{ 
    btn.selected = NO; 
} 

} 

这是按选择选择单选按钮的代码。

-(void)radioBtnPressed : (id)sender event : (id)event 
{ 

NSSet *touches = [event allTouches]; 
UITouch *touch = [touches anyObject]; 
CGPoint currentTouchPosition = [touch locationInView:MyCntryTbl]; 
NSIndexPath *indexPath = [MyCntryTbl indexPathForRowAtPoint: currentTouchPosition]; 

if (indexPath != nil) 
{ 
    appDelegate.countryName = [[appDelegate.MyCntryArr objectAtIndex:indexPath.row] objectForKey:@"Country_Name"]; 
    appDelegate.countryCode = [[appDelegate.MyCntryArr objectAtIndex:indexPath.row] objectForKey:@"Country_Code"]; 

    [MyCntryTbl reloadData]; 
} 
} 

只要所有的事情都是正确的工作,当我在特定单选按钮点击,但现在我想要做桌子上的全细胞中的任何地方点击相同的过程。所以请帮助我。

回答

0

您可以添加的UITableViewDelegate方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    [self radioBtnPressed:event:]; 
} 

,不要忘记的tableView .delegate =自我;

+0

我在这里得到错误'事件'未声明 –

+0

当然,我用抽象[self radioBtnPressed:event:]。你应该调用适当的IBAction“radioButtonClick”。 – Vov4yk

+0

为什么我使用这个,因为触发事件使用指定的值触发点击。 –