Ghobs
Ghobs

Reputation: 859

Subclassing UITableViewCell isn't allowing multiple cell labels

I've subclassed UITableViewCell because rather than a Title and subtitle, I want a title and two separate subtitles, a price and a condition value respectively. I'm building the table programmatically, not in storyboard. I set up the subclassing like so:

MatchCenterCell.h:

#import <UIKit/UIKit.h>

@interface MatchCenterCell : UITableViewCell

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

@end

I then attempt to use it like so:

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;

}

However only the title of the item and the image show in each cell, the PriceLabel and ConditionLabel don't show. Have I subclassed this incorrectly?

Upvotes: 0

Views: 160

Answers (4)

Anurag Bhakuni
Anurag Bhakuni

Reputation: 2439

hope this will help you little more

if your making the xib file for MatchCenterCell

instead of this

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

you should use

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

and use cell.PriceLabel.text = @"text" instead of cell.textLabel.text.

Edited

you are making it all programmatically , then use (1) as you are already doing , ->first check you doing alloc-init of your cell element(label and image)

--- your UITableViewCell subclass -----

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

--- your tableview delegate method --------

{
   .....

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

   [cell.contentView addSubview:cell.PriceLabel];

   .....
}

Note :- The best practice is to feed data in UITableViewCell subclass.

Upvotes: 1

Tamnna
Tamnna

Reputation: 250

Either you can use custom labels like in your code PriceLabel and ConditionLabel or you can use default textlabel like cell.textLabel but not both at same time.so instead of cell.textLabel you can create 3rd label in you cell classes.

Upvotes: 1

meda
meda

Reputation: 45490

Since you have subclass'd UITableViewCell you need to add your own properties add one more instead of 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"];
  • You should make sure you connect every property to its label in MatchCenterCell.
  • The cell should hold 3 labels and they should be connected to these outlets.
  • Do not forget to assign the class to the cell in xCode

Upvotes: -1

Aaron Brager
Aaron Brager

Reputation: 66244

Sounds like either:

  1. PriceLabel and ConditionLabel haven't been added to your cell's contentView, or
  2. Their frames haven't been set properly

Double-check these. #2 you can do either in the cell's layoutSubviews method or by using auto-layout.

Side note: only classes in Objective-C start with an uppercase letter. PriceLabel and ConditionLabel should be priceLabel and conditionLabel.

Upvotes: 0

Related Questions