【问题标题】:How to add/remove uiimage in a uitableviewcell at a specific row如何在特定行的 uitableviewcell 中添加/删除 uiimage
【发布时间】:2014-03-09 09:34:58
【问题描述】:

在下面的代码中,我试图在所选行上显示图像并从先前选择的行中删除图像。在上下滚动表格视图时,所选图像也开始出现在随机行上,而不是仅显示在所选行上。任何人都可以在这里帮我解决它。我正在 ios7 设备上测试这个。 谢谢。

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

    UIImage *image = [UIImage imageNamed:@"btn_checkmark_off.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(270, 15, 17, 17);
    imageView.tag = CELL_IMGVIEW_TG;
    [cell.contentView addSubview:imageView];
}
return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

if (self.prevRowIndex != indexPath.row)
{
    NSIndexPath *pPrevIndexPath = [NSIndexPath indexPathForRow:self.prevRowIndex inSection:indexPath.section];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:pPrevIndexPath];
    UIImageView* pImgView = (UIImageView*)[cell.contentView viewWithTag:CELL_IMGVIEW_TG];
    pImgView.image = [UIImage imageNamed:@"btn_checkmark_off.png"];
    // update index
    self.prevRowIndex = indexPath.row;

}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIImageView* pImgView = (UIImageView*)[cell.contentView viewWithTag:CELL_IMGVIEW_TG];
pImgView.image = [UIImage imageNamed:@"btn_checkmark_on.png"];

}

【问题讨论】:

    标签: iphone uitableview ios5 ios7


    【解决方案1】:

    试试这个:

    - (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];
    
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(270, 15, 17, 17)];
            imageView.tag = CELL_IMGVIEW_TG;
            [cell.contentView addSubview:imageView];
        }
    
        UIImageView* pImgView = (UIImageView*)[cell.contentView viewWithTag:CELL_IMGVIEW_TG];
        if (self.prevRowIndex == indexPath.row) {
            pImgView.image = [UIImage imageNamed:@"btn_checkmark_on.png"];
        }
        else{
            pImgView.image = [UIImage imageNamed:@"btn_checkmark_off.png"];
        }
    
    
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        self.prevRowIndex = indexPath.row;
        [self.tableView reloadData];
    }
    

    您最好将prevRowIndex 替换为selectedRowIndex,这样一目了然。

    【讨论】:

    • 为了删除之前选择的图像,我相信我们需要调用 [self.tableView reloadData] 而不是 reloadRowsAtIndexPaths。对吗?
    【解决方案2】:

    尝试替换这一行:

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    

        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    

    【讨论】:

    • 每次我向上或向下滚动 UITableView 时,它都会创建一个新的 UITableViewCell,如果选择了该行,它的图像就会关闭。可以创建一个新的 UITableViewCell 而不是使用出列的 UITableViewCell 吗?
    猜你喜欢
    • 1970-01-01
    • 2014-07-28
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多