【问题标题】:Button images over the UITableViewCell are getting cleared while scrollingUITableViewCell 上的按钮图像在滚动时被清除
【发布时间】:2011-06-27 09:21:25
【问题描述】:

我在 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)selectRadioButton:(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;
        }
    }
}

【问题讨论】:

  • 你在哪里添加三个 UIButtons ?用什么方法?当滚动时图像被清除时,听起来您在以下方法中做错了什么:-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { }您如何处理代码中的UITableViewCell重用?
  • 是的,您在使用可检查按钮对表格进行编程大约 5 分钟后学到的一点是,表格单元格一旦滚动出视图就会被丢弃。与单元格相关的任何可变数据(例如其检查状态)都必须存储在单独的数组中。如果您最初不知道所需数组的大小,可以使用 NSMutableArray。

标签: uitableview


【解决方案1】:

使用一个数组来存储 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];

根据你的情况使用它并解决你的问题。

【讨论】:

  • 我已经根据上面给出的代码添加了按钮,我有 100 行
  • 100行没问题。当前状态如何,您的问题是否已解决。
  • 不,先生,我无法按照您的代码进行操作。我仍然面临同样的问题..请参阅我用于按钮单击事件的方法。如果需要更正,请修改告诉我。
  • 在您的代码中,您检查复选框是否已选中,否则您在 cellForRowAtIndexPath.no 中设置相同的图像。否则,需要实现检查索引的条件。
  • 先生,我是新手,我已经通过这种方式完成了。连续我只能选择一个按钮。所以只有我这样做了。请告诉我该怎么做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-08
  • 2013-09-27
相关资源
最近更新 更多