Ios 在自定义UITableViewCell中设置属性有困难
我有一个自定义UITableViewCell,其中包含多个标签和一个图像视图,该视图可以是三个图标之一,具体取决于单元格所表示的每个项目中的值。我的所有标签都在正确更新,但图像名称的值始终为零。映像名称声明为NSString类型的属性 自定义单元格称为FavoriteCell,除NSString值“imgName”外,所有值都设置正确 FavoriteCell.h:Ios 在自定义UITableViewCell中设置属性有困难,ios,objective-c,xcode,uitableview,properties,Ios,Objective C,Xcode,Uitableview,Properties,我有一个自定义UITableViewCell,其中包含多个标签和一个图像视图,该视图可以是三个图标之一,具体取决于单元格所表示的每个项目中的值。我的所有标签都在正确更新,但图像名称的值始终为零。映像名称声明为NSString类型的属性 自定义单元格称为FavoriteCell,除NSString值“imgName”外,所有值都设置正确 FavoriteCell.h: @interface FavoriteCell : UITableViewCell @property (nonatomic,
@interface FavoriteCell : UITableViewCell
@property (nonatomic, strong) UILabel *lblMainTitle;
@property (nonatomic, strong) UILabel *lblGaugeID;
@property (nonatomic, strong) UILabel *lblGaugeLastUpdate;
@property (nonatomic, strong) UILabel *lblGaugeHeight;
@property (nonatomic, strong) UILabel *lblGaugeCFS;
@property (nonatomic, strong) UIImage *bgImage;
@property (nonatomic, strong) UIImageView *goodToGoImage;
@property (nonatomic, strong) NSString *imgName;
@end
FavoriteCell.m:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
bgImage = [UIImage imageNamed:@"tableCellBG.png"];
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UIColor *transparentBG = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
CGSize size = self.contentView.frame.size;
//self.backgroundView = [[UIImageView alloc] initWithImage:bgImage];
self.lblMainTitle = [[UILabel alloc] initWithFrame:CGRectMake(8.0, -10, size.width-20, size.height-40)];
[self.lblMainTitle setFont:[UIFont boldSystemFontOfSize:15]];
[self.lblMainTitle setTextAlignment:NSTextAlignmentLeft];
[self.lblMainTitle setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
[self.lblMainTitle setBackgroundColor:transparentBG];
self.lblGaugeID = [[UILabel alloc] init];
self.lblGaugeLastUpdate = [[UILabel alloc] initWithFrame:CGRectMake(9, 14, size.width-40, size.height)];
[self.lblGaugeLastUpdate setFont:[UIFont systemFontOfSize:14]];
[self.lblGaugeLastUpdate setBackgroundColor:transparentBG];
self.lblGaugeHeight = [[UILabel alloc] initWithFrame:CGRectMake(8, 45, size.width-40, size.height)];
[self.lblGaugeHeight setFont:[UIFont boldSystemFontOfSize:21]];
[self.lblGaugeHeight setBackgroundColor:transparentBG];
self.lblGaugeCFS = [[UILabel alloc] initWithFrame:CGRectMake(120, 45, size.width-40, size.height)];
[self.lblGaugeCFS setFont:[UIFont boldSystemFontOfSize:21]];
[self.lblGaugeCFS setBackgroundColor:transparentBG];
NSLog(@"IMAGE NAME: %@\n", imgName); // imgName is nil
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 40, 40, 40)];
[imgView setImage:[UIImage imageNamed:imgName]];
[self.contentView addSubview:lblMainTitle];
[self.contentView addSubview:lblGaugeHeight];
[self.contentView addSubview:lblGaugeLastUpdate];
[self.contentView addSubview:lblGaugeCFS];
[self.contentView addSubview:imgView];
self.editingAccessoryType = UITableViewCellAccessoryDetailButton;
}
return self;
}
这些值是在此处显示的my table view控制器的CellForRowatineXpath方法中设置的。现在,我只是为测试硬编码一个值。硬编码值未结转至自定义单元格
-(FavoriteCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
FavoriteCell *cell = (FavoriteCell*)[tableView dequeueReusableCellWithIdentifier:cellID];
if(cell == nil){
cell = [[FavoriteCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
Favorite *fave = [allFavoriteObjects objectAtIndex:indexPath.row];
NSString *currFeet = [NSString stringWithFormat:@"%.02fft", [fave.currentFeet doubleValue]];
NSString *currFlow = [NSString stringWithFormat:@"%dcfs", [fave.currentCFS intValue]];
[cell setImgName:@"thumbsUp.png"];
[cell.lblMainTitle setText:[fave stationRealName]];
[cell.lblGaugeID setText:[fave stationIdentifier]];
[cell.lblGaugeCFS setText:currFlow];
[cell.lblGaugeHeight setText:currFeet];
return cell;
}
我错过了什么明显的东西吗?谢谢 好吧,乍一看,你说
imgName
的那一刻记录为nil,似乎是因为在调用initWithStyle
之后,你才真正设置imgName
。或者我对您期望发生的事情有什么遗漏吗?好吧,乍一看,您说imgName
的那一刻似乎是因为在调用initWithStyle
之前,您没有实际设置imgName
。或者我对您期望发生的事情有什么遗漏吗?这是正常的,因为当您没有设置imgName时,代码正在运行,您设置了imgName,然后运行init代码。
fox fix it从自定义单元格中删除代码UIImage和NSString,并将其设置到您的imageView中,然后当您将单元格设置到
-tableView:cellForRowAtIndexPath:
byimageView.image=[UIImage ImageName:@“thumbsUp.png]”中时代码>这是正常的,因为当您没有设置imgName时,代码正在运行,您可以设置它,然后运行init代码。
fox fix it从自定义单元格中删除代码UIImage和NSString,并将其设置到您的imageView中,然后当您将单元格设置到-tableView:cellForRowAtIndexPath:
byimageView.image=[UIImage ImageName:@“thumbsUp.png]”中时代码>使用UITableViewCell的标准imageView属性显然不允许自定义大小和位置。您不明白我的意思。对,他在init
方法中创建一个单独的UIImageView
,然后将其作为子视图添加到contentView
是的,但他要做的是在init中创建一个UIImageView,然后将其添加到tableView:cellForRowAtIndexPath:设置其图像(并且不像他那样进入init,因为当init运行时,变量image没有设置)我的解释很糟糕,但这是正常的,字符串imgName为零,因为使用UITableViewCell的标准imageView属性设置init显然不允许自定义大小和位置。你不明白我的意思。对,他在init
方法中创建了一个单独的UIImageView
,然后将其作为子视图添加到contentView
yes,但他必须在init中创建一个UIImageView,然后放入tableView:cellforrowatinedexpath:设置其映像(而不是像他那样放入init,因为当init运行时,变量映像没有设置)我的解释很糟糕,但这是正常的,字符串imgName为nil,因为设置了init,那么什么是GoodToGramage
用于?如果您想稍后更改自定义UIImageView
,您可能需要使用一个属性。在init
方法中,您只需创建UIImageView
然后在其上设置图像,但是不要将指向视图的指针保存到任何地方。(也许这就是goodogimage
的作用?)GoodTogImage
用于什么?如果以后要更改自定义的UIImageView
,可能需要使用属性。在init
方法中,您只需创建一个UIImageView
,然后在其上设置图像,但不要将指向视图的指针保存在任何位置。(也许这就是goodToGoImage
的作用?)