2011-10-11 10 views
1

私のアプリケーションでは、テーブルビューの各行にラジオボタンを実装しています。 問題は、1行目のラジオボタンをクリックしているときです。つまり、5行目のラジオボタンが選択されています。ラジオボタンをクリックしたとき

私がクリックしているボタンを選択する必要があります。ここで私がしたコードです。

ありがとうございます。

(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

このコードにはいくつか問題があります。

1)

あなたは[のtableView dequeueReusableCellWithIdentifier]は、実際に使用するために有効なセルを返している場合を処理していないされ、代わりにあなたがいる場合のみ、「セル== NULL」の仕事をやっています。

ブレークポイントを設定してindexPath.rowを参照すると、 "cell == NULL"の場合のコードは散発的にしか呼び出されません。

2)

"btn"とは何ですか?それを割り当ててから何もしないでください。

関連する問題