2011-07-01 134 views
1

我使用UITableView单元格上的单选按钮。首先单选按钮未选中。当单选按钮被检查时。我正在滚动UITableView单选按钮未选中。使用此代码如下当单选按钮选择检查

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 


    return 100; 

} 

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

    static NSString *CellIdentifier = @"Cell"; 
    static NSString *CellIdentifierFirst = @"CellFirst"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierFirst]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease]; 
     //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil] autorelease]; 
    } 

    NSArray *cellSubs = cell.contentView.subviews; 
    for (int i = 0 ; i < [cellSubs count] ; i++) { 
     [[cellSubs objectAtIndex:i] removeFromSuperview]; 
    } 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    UIButton *redioBtn1 = [[UIButton alloc]initWithFrame:CGRectMake(20, 20, 20, 20)]; 
    [redioBtn1 setImage:[UIImage imageNamed:@"radiounselect.png"] forState:UIControlStateNormal]; 
    [redioBtn1 addTarget:self action:@selector(selectRadioButon:) forControlEvents:UIControlEventTouchUpInside]; 
    redioBtn1.tag = ++tagCount; 
    [cell.contentView addSubview:redioBtn1]; 

    UIButton *redioBtn2 = [[UIButton alloc]initWithFrame:CGRectMake(80, 20, 20, 20)]; 
    [redioBtn2 setImage:[UIImage imageNamed:@"radiounselect.png"] forState:UIControlStateNormal]; 
    [redioBtn2 addTarget:self action:@selector(selectRadioButon:) forControlEvents:UIControlEventTouchUpInside]; 
    redioBtn2.tag = ++tagCount; 
    [cell.contentView addSubview:redioBtn2]; 

    UIButton *redioBtn3 = [[UIButton alloc]initWithFrame:CGRectMake(140, 20, 20, 20)]; 
    [redioBtn3 setImage:[UIImage imageNamed:@"radiounselect.png"] forState:UIControlStateNormal]; 
    [redioBtn3 addTarget:self action:@selector(selectRadioButon:) forControlEvents:UIControlEventTouchUpInside]; 
    redioBtn3.tag = ++tagCount; 
    [cell.contentView addSubview:redioBtn3]; 

    return cell; 


} 


-(void)selectRadioButon:(id)sender { 


    btnTag = [sender tag]; 
    NSArray *arr = self.view.subviews; 
    UITableView *tblCell = [arr objectAtIndex:0]; 
    NSArray *cellAry = tblCell.subviews; 

    for (int i = 0; i <[cellAry count]; i++) { 
     UITableViewCell *content = [cellAry objectAtIndex:i]; 
     NSArray *contentArry = content.contentView.subviews; 
     for (int j = 0; j <[contentArry count]; j++) { 
      UIButton *button = [contentArry objectAtIndex:j]; 
      if (btnTag == button.tag) { 
       for (int i = 0; i <[contentArry count]; i++) { 
        UIButton *button = [contentArry objectAtIndex:i]; 
        if (btnTag == button.tag) { 
         if ([sender currentImage]==[UIImage imageNamed:@"radioselect.png"]) 

          [button setImage:[UIImage imageNamed:@"radiounselect.png"] forState:UIControlStateNormal]; 
         else 
          [button setImage:[UIImage imageNamed:@"radioselect.png"] forState:UIControlStateNormal]; 
        } 
        else 
         [button setImage:[UIImage imageNamed:@"radiounselect.png"] forState:UIControlStateNormal]; 

       } 

       return; 
      } 
     } 

    } 


} 

回答

0

第一件事:你不应该添加的UIButton的每次细胞加载。您应该将它们添加到if (cell == nil) block之内。

接下来的事情:你应该保持在一个sepearate阵列(一个NSMutableArray的)磁道为所选择的单选按钮的索引(一个INT值),并选择相应的UIButton,所述if (cell == nil) block外部。

我希望this tutorial会帮助你。