滚动时,UITableViewCell上的按钮图像将被清除

时间:2011-06-27 09:21:26

标签: uitableview

我在UITableView的每个单元格中添加了三个带背景图像的UIButtons作为单选按钮未选中图像,当有人点击它时按钮图像将更改为单选按钮选中图像但问题是当我滚动UITableView时选中的按钮图像我正在滚动时被清除。

有人可以给我任何想法..!

这是我声明UIButtons的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
static NSString *CellSetup = @"CellSetup";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellSetup] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.tag=[indexPath row];

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

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

myButton3 = [[UIButton alloc]initWithFrame:CGRectMake(140, 20, 20, 20)]; 
[myButton3 setImage:[UIImage imageNamed:@"radiounselect.png"] forState:UIControlStateNormal];
[myButton3 addTarget:self action:@selector(selectRadioButon:) forControlEvents:UIControlEventTouchUpInside];
myButton3.tag = tagCount;
[cell.contentView addSubview:myButton3];
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 *contentAry = content.contentView.subviews;
    for (int j = 0; j <[contentAry count]; j++) {
        UIButton *button = [contentAry objectAtIndex:j];
        if (btnTag == button.tag) {
            for (int k = 0; k <[contentAry count]; k++) {
                UIButton *button = [contentAry objectAtIndex:k];
                if (btnTag == button.tag) {
                    [button setImage:[UIImage imageNamed:@"radioselect.png"] forState:UIControlStateNormal];
                }
                else
                    [button setImage:[UIImage imageNamed:@"radiounselect.png"] forState:UIControlStateNormal];
            }
            return;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

使用数组存储indexpath.row以显示chceked复选框,请参阅此代码

-(void)goodClick:(UIButton *)sender
{
    NSString *index = sender.titleLabel.text ;
    if([self.goodIndexArray containsObject:index]){
        //[sender setImage:[UIImage imageNamed:@"uncheckedRect.png"] forState:UIControlStateNormal];
        [self.goodIndexArray removeObject:index];

    }
    else
    {
        //[sender setImage:[UIImage imageNamed:@"checkedRect.png"] forState:UIControlStateNormal];
        [self.goodIndexArray addObject:index];
        if([self.badIndexArray containsObject:index])
            [self.badIndexArray removeObject:index];

    }
    [self done];
    [self.table reloadData];
}

添加此方法作为你的checbox按钮的选择器,并为你的按钮indexPath.row添加标题。

这为您提供了识别已选中复选框的单元格的方法。

并在cellForRowAtIndexPath方法中实现检查。

查看此检查条件

if([self.goodIndexArray containsObject:[NSString stringWithFormat:@"%i",coun]])
            {
                [goodButton setImage:[UIImage imageNamed:@"checkedRect.png"] forState:UIControlStateNormal];
            }
            else
                [goodButton setImage:[UIImage imageNamed:@"uncheckedRect.png"] forState:UIControlStateNormal];

根据您使用此功能,解决您的问题。