ios - 无法刷新 UITableView 内的图片

标签 ios objective-c cocoa-touch cocoa

我正在尝试更改桌面 View 中的图片。我在另一种方法(正在启动)中使用 [myTableView reloadData]; 来刷新我的 TableView ,我可以看到 tableview 中的其他元素正在刷新,但图像保持不变。我错过了什么?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UILabel *lblName;
    UIImageView *checkMark = [[UIImageView alloc] initWithFrame:CGRectMake(230, 0, 30, 50)];
    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(10, 1, 260, 70)];
        backgroundView.backgroundColor = UIColorFromRGB(0x000000, 0.3);
        backgroundView.userInteractionEnabled = YES;        

        checkMark.image = [UIImage imageNamed:@"V.png"];
        checkMark.contentMode = UIViewContentModeCenter;
        checkMark.backgroundColor = [UIColor clearColor];
        [backgroundView addSubview:checkMark];
        [checkMark release];

        [cell.contentView addSubview:backgroundView];
        [backgroundView release];
    }
    if (something)
    {
        checkMark.image = [UIImage imageNamed:@"halfleft.png"];
        NSLog(@"something was done");
    }
    return cell;

}

最佳答案

reloadData 正确调用此方法。

您的问题很简单,在单元格被“重用”的情况下,“checkMark”上的指​​针不好。做这样的事情:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UIView *backgroundView;
    UIImageView *checkMark;
    if (cell == nil) {

        checkMark = [[UIImageView alloc] initWithFrame:CGRectMake(230, 0, 30, 50)];
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        backgroundView = [[UIView alloc] initWithFrame:CGRectMake(10, 1, 260, 70)];
        backgroundView.backgroundColor = UIColorFromRGB(0x000000, 0.3);
        backgroundView.userInteractionEnabled = YES;
        backgroundView.tag = 555;

        checkMark.image = [UIImage imageNamed:@"V.png"];
        checkMark.contentMode = UIViewContentModeCenter;
        checkMark.backgroundColor = [UIColor clearColor];
        checkMark.tag = 666;
        [backgroundView addSubview:checkMark];
        [checkMark release];

        [cell.contentView addSubview:backgroundView];
        [backgroundView release];
    } else {
        backgroundView = (UIImageView *)[cell.contentView viewWithTag:555];
        checkMark = (UIImageView *)[backgroundView viewWithTag:666];
    }
    if (something)
    {
        checkMark.image = [UIImage imageNamed:@"halfleft.png"];
        NSLog(@"something was done");
    }
    return cell;

}

但是,您应该做一些更简单的事情(如果可能的话,减少 subview 。或者子类 UITableViewCell 以在复选标记上有一个指针;-)

关于ios - 无法刷新 UITableView 内的图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14919759/

相关文章:

ios - Google Maps SDK 放置标记

iphone - iPad UITableView 闪烁问题

iphone - 当我更改日期选择器上的时间时应用程序崩溃

objective-c - 从 Notepad++ 自动化 GNUStep

cocoa-touch - 如何根据 ASIHTTPRequest 执行不同的操作?

ios - 使用 Unity 运行时无法在 iPhone 上使用相机

ios - 带有 CGContextStrokePath 的虚线

objective-c - iOS UITableView numberOfRowsInSection 错误访问

objective-c - 错误 : ‘NSString’ undeclared (first use in this function)

ios - 为什么CloudKit中批量删除记录的CKModifyRecordsOperation没有删除记录?