【发布时间】:2014-07-13 10:27:16
【问题描述】:
所以基本上我有一个自定义 UITableViewCell(在这种情况下是一个简单的单元格,中间设置了 UIImageView),它被正确子类化了(我有一个单元格的 .h .m 文件,并且图像视图被正确地“输出”了)。
在加载单元格时,我需要能够将 UIImageView 更改为圆形并向 UIImageView 添加手势识别器(这样如果用户点击 ImageView,它将加载 UIImagePicker - 允许用户设置图像)。
我面临的问题是,尽管代码是根据调试器执行的,但 UI 从未更新 - (我仍然看到一个通用的方形 UIImageView 并且手势识别器不起作用)。
下面是 cellforrowatindexpath 委托方法的代码:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row==0 && indexPath.section==0) {
static NSString *CellIdentifier = @"Profile Cell";
Profileviewcell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[Profileviewcell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.ProfileImage.layer.cornerRadius = cell.ProfileImage.bounds.size.width/2;
cell.ProfileImage.layer.masksToBounds = YES;
[cell.ProfileImage setUserInteractionEnabled:YES];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(LoadImage:)];
tap.cancelsTouchesInView = YES;
tap.numberOfTapsRequired = 1;
tap.delegate = self;
[cell.ProfileImage addGestureRecognizer:tap];
[cell setNeedsDisplay];
return cell;
} else {
//return a standard cell
}
}
我也尝试在 willdisplaycell 委托方法中放入相同的代码(以及 [cell setNeedsdisplay] 但这也不起作用。
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row==0 && indexPath.section==0) {
Profileviewcell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.ProfileImage.layer.cornerRadius = cell.ProfileImage.bounds.size.width/2;
cell.ProfileImage.layer.masksToBounds = YES;
[cell.ProfileImage setUserInteractionEnabled:YES];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(LoadImage:)];
tap.cancelsTouchesInView = YES;
tap.numberOfTapsRequired = 1;
tap.delegate = self;
[cell.ProfileImage addGestureRecognizer:tap];
[cell setNeedsDisplay];
}
}
Initwithstyle 代码:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.ProfileImage = [[UIImageView alloc] initWithFrame:CGRectMake(110,5,100,100)];
[self.ProfileImage setImage:[UIImage imageNamed:@"General Profile"]];
self.ProfileImage.layer.cornerRadius = self.ProfileImage.bounds.size.width/2;
self.ProfileImage.layer.masksToBounds = YES;
[self.ProfileImage setUserInteractionEnabled:YES];
[self.contentView addSubview:self.ProfileImage];
}
return self;
}
关于为什么单元格 UI 永远不会更新的任何想法?我已经检查了 Stack Overflow 是否存在类似问题,并且提供的每个解决方案似乎都不适合我。抱歉,如果这是一个简单的问题 - 我最近才开始 iOS 开发。
非常感谢!
【问题讨论】:
-
修改
layer并添加手势识别器时cell.ProfileImage是否等于nil?如果是这样,那将说明您遇到的问题。 -
@bobnoble 非常感谢您的回复!是的,根据调试器它为零,但我不完全确定为什么!该单元格是情节提要中的原型单元格,我也在 Xcode 中创建了 UITableViewCell 的子类。我将如何阻止它为零(为什么它首先是零?)?
-
显示自定义
UITableViewCell的代码,尤其是在声明ProfileImage的位置以及如何将其初始化为initWithStyle方法的一部分。 -
dequeueReusableCellForIdentifier:将始终返回一个有效的单元格。你不需要检查它是否是nil。 -
@bobnoble 为迟到的回复道歉 - 我已经编辑了原始问题以包含 initwithStyle 方法(它太大,无法在此处发布) - 谢谢!
标签: ios uitableview uiimageview