尝试通过 viewWithTag 获取 UIImageView 引用后崩溃
我需要在表格单元格中绘制图像。到目前为止,在创建视图并将其分配给单元格后,我无法获得对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于 imageView 变量的范围。如果单元格尚不存在,您将创建一个仅存在于 if 块中的新
UIImageView
。它隐藏了您之前声明的变量,并在 if 块结束后消失。相反,
您应该简单地编写
否则,您将创建一个与您在方法顶部声明的对象无关的新对象,并且原始
imageView
仍然未定义。The problem is the scope of the
imageView
variables. In the case that the cell doesn't exist yet, you create a newUIImageView
that only exists in the if-block. It hides the variable that you declared earlier and disappears after the if-block ends.Instead of
you should simply write
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.