【问题标题】:Setting Image in custom UITableviewCell does not *Stick*在自定义 UITableviewCell 中设置图像不会 *Stick*
【发布时间】:2012-06-24 20:15:33
【问题描述】:

我有一个自定义的 UITabvleViewCell,其中有一个 UIImageView。当在 cellForRowAtIndexPath 中设置单元格时,我可以很好地设置图像(尽管我没有)但是在某些条件下我需要更改图像并且一直在使用以下代码进行更改。

-(void)updateCellForUPC
{
    AppData* theData = [self theAppData];

    NSInteger cellIndex;
    for (cellIndex = 0; cellIndex < [productArray count]; cellIndex++) {

      NSString *cellUPC = [NSString stringWithFormat:@"%@",[[productArray objectAtIndex:cellIndex] objectForKey:@"UPC"]];

        if ([cellUPC isEqualToString:theData.UPC]) {

            OrderDetailCell *activeCell = [[OrderDetailCell alloc] init];
            activeCell = (OrderDetailCell *) [orderDetailTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:cellIndex inSection:0]];

            UIImage* image = [UIImage imageNamed:@"checkmark.png"];
            activeCell.statusImageView.image = image;
            activeCell.checked = YES;
        }
    }
}

这很有效,并且图像会更新,但是当您将单元格从屏幕上滚动并返回到屏幕上时,图像会被重置!

我需要它来*坚持新图像。

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    不建议这样做,因为您没有从未使用单元的出队机制中获益。在创建单元格时考虑这样做,或者创建一个知道应该对哪些单元格执行此操作的机制。

    【讨论】:

    • 当我创建它需要的单元格时,我无法做到这一点。那么,您是否建议在 cellForRowAtIndexPath 中根据索引 + 一些外部属性设置图像然后刷新 tableview?可能是另一个数组,需要它的单元格的索引被添加到?
    • 感谢您的帮助,如果您好奇,我在下面有答案。
    【解决方案2】:

    对于任何对我将来如何做到这一点感兴趣的人。

    我创建了一个 NSMutableArray 并将索引添加为 NSString。如果当时正在显示单元格,我还设置了单元格的图像。

    -(void)updateCellForUPC
    {
        AppData* theData = [self theAppData];
    
        NSInteger cellIndex;
        for (cellIndex = 0; cellIndex < [productArray count]; cellIndex++) {
    
          NSString *cellUPC = [NSString stringWithFormat:@"%@",[[productArray objectAtIndex:cellIndex] objectForKey:@"UPC"]];
    
            if ([cellUPC isEqualToString:theData.UPC]) {
    
                OrderDetailCell *activeCell = [[OrderDetailCell alloc] init];
                activeCell = (OrderDetailCell *) [orderDetailTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:cellIndex inSection:0]];
    
                UIImage* image = [UIImage imageNamed:@"checkmark.png"];
                activeCell.statusImageView.image = image;
    
                NSString *index = [NSString stringWithFormat:@"%d",cellIndex];
                [selectionArray addObject:index];
            }
        }
    }
    

    然后在我的 cellForRowAtIndexPath 中,我只使用以下代码来检查索引是否在数组中,如果是则设置我的图像。这样,当单元格被重绘(滚动)时,它就有了图像。

    NSString *index = [NSString stringWithFormat:@"%d",indexPath.row];
    
    if ([selectionArray containsObject:index]) {
        UIImage* image = [UIImage imageNamed:@"checkmark.png"];
        cell.statusImageView.image = image;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-22
      • 2012-01-20
      • 1970-01-01
      相关资源
      最近更新 更多