尝试通过 viewWithTag 获取 UIImageView 引用后崩溃

发布于 2024-11-06 20:36:05 字数 1379 浏览 0 评论 0原文

我需要在表格单元格中绘制图像。到目前为止,在创建视图并将其分配给单元格后,我无法获得对 UIImageView 的正确引用。例如,相同的过程适用于 UILabel。

我不明白我做错了什么。

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UIImageView *imageView;
    UILabel *title;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                       reuseIdentifier:CellIdentifier] autorelease];

        // Setup title
        title = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)] autorelease];
        title.tag = 1;
        [cell.contentView addSubview:title];

        // Setup image
        UIImageView* imageView = [[[ UIImageView alloc] initWithFrame: 
                                   CGRectMake(50, 0, 50, 50)] autorelease];
        imageView.tag = 2;
        [cell.contentView addSubview:imageView];

    } else {
        // Get references to cell views
        title = (UILabel *)[cell.contentView viewWithTag:1];
        imageView = (UIImageView *)[cell.contentView viewWithTag:2];
    }

    NSLog(@"%@", [title class]);     // UILabel
    NSLog(@"%@", [imageView class]); // CRASH! EXC_BAD_ACCESS

    return cell;
}

I need to draw an image in a table cell. So far I have not been able to get a proper reference to the UIImageView after I create the view and assign it to the cell. The same procedure does work for a UILabel, for example.

I can't figure out what I'm doing wrong.

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UIImageView *imageView;
    UILabel *title;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                       reuseIdentifier:CellIdentifier] autorelease];

        // Setup title
        title = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)] autorelease];
        title.tag = 1;
        [cell.contentView addSubview:title];

        // Setup image
        UIImageView* imageView = [[[ UIImageView alloc] initWithFrame: 
                                   CGRectMake(50, 0, 50, 50)] autorelease];
        imageView.tag = 2;
        [cell.contentView addSubview:imageView];

    } else {
        // Get references to cell views
        title = (UILabel *)[cell.contentView viewWithTag:1];
        imageView = (UIImageView *)[cell.contentView viewWithTag:2];
    }

    NSLog(@"%@", [title class]);     // UILabel
    NSLog(@"%@", [imageView class]); // CRASH! EXC_BAD_ACCESS

    return cell;
}

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

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

发布评论

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

评论(1

つ低調成傷 2024-11-13 20:36:05

问题在于 imageView 变量的范围。如果单元格尚不存在,您将创建一个仅存在于 if 块中的新 UIImageView。它隐藏了您之前声明的变量,并在 if 块结束后消失。
相反,

UIImageView *imageView = ...

您应该简单地编写

imageView = ...

否则,您将创建一个与您在方法顶部声明的对象无关的新对象,并且原始 imageView 仍然未定义。

The problem is the scope of the imageView variables. In the case that the cell doesn't exist yet, you create a new UIImageView that only exists in the if-block. It hides the variable that you declared earlier and disappears after the if-block ends.
Instead of

UIImageView *imageView = ...

you should simply write

imageView = ...

Otherwise you're creating a new object that has nothing to do with the object you declared at the top of the method and the original imageView is still undefined.

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