子类化UITableViewCell不允许多个单元格标签

时间:2014-11-12 04:43:58

标签: ios objective-c uitableview

我已经将UITableViewCell子类化,因为我不是标题和副标题,而是分别需要标题和两个单独的字幕,价格和条件值。 我正在以编程方式构建表,而不是在storyboard中构建。我像这样设置了子类:

MatchCenterCell.h:

#import <UIKit/UIKit.h>

@interface MatchCenterCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UILabel *PriceLabel;
@property (strong, nonatomic) IBOutlet UILabel *ConditionLabel;

@end
然后我尝试使用它:

MatchCenterViewController.m:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Initialize cell
    static NSString *CellIdentifier = @"MatchCenterCell";
    MatchCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        // if no cell could be dequeued create a new one
        cell = [[MatchCenterCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    tableView.separatorColor = [UIColor clearColor];

    NSDictionary *currentSectionDictionary = _matchCenterArray[indexPath.section];
    NSArray *top3ArrayForSection = currentSectionDictionary[@"Top 3"];

    if (top3ArrayForSection.count-1 < 1) {

        // title of the item
        cell.textLabel.text = @"No items found, but we'll keep a lookout for you!";
        cell.textLabel.font = [UIFont systemFontOfSize:12];

        // price of the item
        cell.detailTextLabel.text = @"";

        // image of the item
        NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@""]];
        [[cell imageView] setImage:[UIImage imageWithData:imageData]];

    }

    else {

        // title of the item
        cell.textLabel.text = _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Title"];
        cell.textLabel.font = [UIFont systemFontOfSize:14];

        // price of the item
        cell.PriceLabel.text = [NSString stringWithFormat:@"$%@", _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Price"]];
        cell.PriceLabel.textColor = [UIColor colorWithRed:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f];

        // condition of the item
        cell.ConditionLabel.text = [NSString stringWithFormat:@"$%@", _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Item Condition"]];
        cell.ConditionLabel.textColor = [UIColor colorWithRed:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f];

        // image of the item
        NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Image URL"]]];
        [[cell imageView] setImage:[UIImage imageWithData:imageData]];

        cell.imageView.layer.masksToBounds = YES;
        cell.imageView.layer.cornerRadius = 2.5;

    }

    return cell;

}

但是,只有每个单元格中的项目标题和图片显示,PriceLabelConditionLabel都不显示。我是否错误地将其分类?

4 个答案:

答案 0 :(得分:1)

您可以使用自己的代码PriceLabel和ConditionLabel中的自定义标签,也可以使用默认的textlabel,如cell.textLabel,但不能同时使用两者。而不是cell.textLabel,您可以在单元格类中创建第三个标签。 / p>

答案 1 :(得分:1)

希望这对你有所帮助

如果您为MatchCenterCell制作xib文件

而不是

cell = [[MatchCenterCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];---------- (1)

你应该使用

cell = (MatchCenterCell *) [[[NSBundle mainBundle] loadNibNamed:@"MatchCenterCell" owner:self options:nil] lastObject]; -------- (2)

并使用cell.PriceLabel.text = @"text"代替cell.textLabel.text

已修改

您正在以编程方式完成所有操作,然后使用(1)正如您已经在做的那样, - &gt;首先检查你的cell元素的alloc-init(标签和图像)

---您的UITableViewCell子类-----

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

       self.PriceLabel  = [[ UILabel alloc]initWithFrame:CGRectMake(10, 10, 30, 40)];

   }
    return self;
}

---你的tableview委托方法--------

{
   .....

cell.PriceLabel.text = [NSString stringWithFormat:@"$%@", _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Price"]];

   [cell.contentView addSubview:cell.PriceLabel];

   .....
}

Note: - 最佳做法是在UITableViewCell子类中提供数据。

答案 2 :(得分:0)

听起来像是:

  1. PriceLabelConditionLabel尚未添加到您的单元格contentView
  2. 他们的帧未正确设置
  3. 仔细检查这些。 #2您可以使用单元格的layoutSubviews方法或使用自动布局。

    旁注:只有Objective-C中的类以大写字母开头。 PriceLabelConditionLabel应为priceLabelconditionLabel

答案 3 :(得分:-1)

由于你有子类&#39; d UITableViewCell,你需要添加一个属性而不是textLabel

<强> MatchCenterCell.h:

@property (strong, nonatomic) IBOutlet UILabel *subtitleLabel;
@property (strong, nonatomic) IBOutlet UILabel *priceLabel;
@property (strong, nonatomic) IBOutlet UILabel *conditionLabel;

<强> MatchCenterCell.m:

cell.subtitleLabel.text = 
           _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Title"];
  • 您应该确保将每个属性连接到其标签中 MatchCenterCell
  • 单元格应该有3个标签,它们应该连接到这些标签 网点。
  • 不要忘记将类分配给xCode
  • 中的单元格
相关问题