2011-10-07 36 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

爲什麼我使用這個,因爲觸發事件使用指定的值觸發點擊。 –

相關問題