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

发布于 2024-11-17 13:36:51 字数 2778 浏览 0 评论 0原文

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

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

的代码

- (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;}

这是我声明 UIButtons -(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;
        }
    }
}

I have added three UIButtons with background image as radio button unchecked image in the UITableView's each cell and when Somebody clicks it the buttons image will change to radio buttons checked image but the problem is when I am scrolling the UITableView the checked buttons images are getting cleared while I am scrolling.

Can somebody please give me any idea..!

This the code in which I have declared 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;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慕巷 2024-11-24 13:36:51

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

-(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];
}

添加此方法作为您的复选框按钮的选择器并为您的按钮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];

根据您的情况使用此并解决您的问题。

use an array for storing indexpath.row for showing chceked check boxes see this code

-(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];
}

Add this method as your selector for your checbox button and add title for you button indexPath.row.

This provide you the way for identifying the cell which having checked check boxes.

And implement checks in cellForRowAtIndexPath method.

see this check conditions

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];

Use this according to you and get your problem solved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文