ios - 难以在自定义 UITableViewCell 中设置属性

标签 ios objective-c xcode uitableview properties

我有一个自定义的 UITableViewCell,它包含多个标签和一个 ImageView , ImageView 可以是三个图标之一,具体取决于单元格表示的每个项目中的值。我所有的标签都正确更新,但图像名称的值始终为零。图像名称被声明为 NSString 类型的属性。

自定义单元格称为 FavoriteCell,除 NSString 值“imgName”外,所有值都设置正确

FavoriteCell.h:

@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;
}

这些值是在我的 TableView Controller 的 cellForRowAtIndexPath 方法中设置的,此处显示。现在,我只是硬编码一个测试值。硬编码值不会转移到自定义单元格。

-(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 时你的代码正在运行,你设置它然后你的初始化代码运行。 fox fix 它从您的自定义单元格中删除代码 UIImage 和 NSString 并将其设置到您的 imageView 中,然后当您将单元格放入 -tableView:cellForRowAtIndexPath:imageView.image = [UIImage imageNamed:@"thumbsUp.png"];

关于ios - 难以在自定义 UITableViewCell 中设置属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19639708/

相关文章:

ios - Audio Session 中断结束后 AVAudioRecorder 不在后台录制

iphone - 重新加载表的数据

iOS,如何使用GMSCoordinateBounds显示 map 的所有标记?

ios - 通知有效但没有声音

iphone - 通过第一响应者更改保持 iPhone 键盘对齐的工具栏可见?

iphone - 在演示或解散过程中尝试从 View Controller 中解散

ios - 是否可以在 Swift 中为特定的 iOS 版本定义一个变量?在类(class)层面

ios - 如何下载旧版本的 iPhone SDK?

objective-c - 我如何禁用 touchesBegan : for multi-touches?

swift - Xcode 14 Xcode 13 不兼容