3

I have this code segment:

if (cell == nil)
{
    CGRect cellFrame = CGRectMake(0,0,300,250);
    cell = [[UITableViewCell alloc] initWithFrame:cellFrame
            reuseIdentifier:CellTableIndetifier];

    CGRect nameLabelRect = CGRectMake(0, 5, 70, 20);
    UILabel* nameLabel = [[UILabel alloc] initWithFrame:nameLabelRect];
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.text = @"Name";
    nameLabel.font = [UIFont boldSystemFontOfSize:12];
    [cell.contentView addSubview: nameLabel];

    CGRect colorLabelRect = CGRectMake(0, 25, 70, 20);
    UILabel* colorLabel = [[UILabel alloc] initWithFrame:colorLabelRect];
    colorLabel.textAlignment = NSTextAlignmentCenter;
    colorLabel.text = @"Color";
    colorLabel.font = [UIFont boldSystemFontOfSize:12];
    [cell.contentView addSubview: colorLabel];

    CGRect priceLabelRect = CGRectMake(0, 45, 70, 20);
    UILabel *priceLabel = [[UILabel alloc] initWithFrame:priceLabelRect];
    priceLabel.text = @"Price";
    priceLabel.textAlignment = NSTextAlignmentCenter;
    colorLabel.font = [UIFont boldSystemFontOfSize:12];
    [cell.contentView addSubview:priceLabel];

    CGRect nameValueRect = CGRectMake(80, 5, 200, 20);
    UILabel* nameValue = [[UILabel alloc] initWithFrame: nameValueRect];
    nameValue.tag = kNameValueTag;
    [cell.contentView addSubview:nameValue];

    CGRect colorValueRect = CGRectMake(80, 25, 200, 20);
    UILabel* colorValue = [[UILabel alloc] initWithFrame:colorValueRect];
    colorValue.tag = kColorValueTag;
    [cell.contentView addSubview:colorValue];

    CGRect priceValueRect = CGRectMake(80, 45, 200, 20);
    UILabel *priceValue = [[UILabel alloc] initWithFrame:priceValueRect];
    priceValue.tag = kPriceValueTag;
    [cell.contentView addSubview:priceValue];
}

and I would like to make that make that into a subclass, so I don't have to write all those lines, I just say cell = CustomCell and it does everything in the subclass.

woz
  • 10,888
  • 3
  • 34
  • 64
Lord Zsolt
  • 6,492
  • 9
  • 46
  • 76
  • What part of subclassing are you having trouble with? – woz Jun 21 '13 at 12:14
  • It's not obvious to me how subclassing would help; you still need to position the cell and assign tags, which are unique for each cell... – trojanfoe Jun 21 '13 at 12:16
  • I kinda don't know to to create a subclass. I'm a little new to Xcode so I don't fully understand the syntax. – Lord Zsolt Jun 21 '13 at 12:16
  • 4
    You mean you are new to Objective-C; Xcode is an IDE and doesn't have a "syntax". You should remove Xcode from the title of this question. (H2CO3 will be here any minute, so be quick)... – trojanfoe Jun 21 '13 at 12:17
  • Have you Googled for some tutorials? – woz Jun 21 '13 at 12:22
  • @trojanfoe: I don't understand your first comment. All the label positions (relative to the cell) and tags are identical for each cell, so it can be done in the subclass `initWithStyle:reuseIdentifier:` method. - Perhaps I am overlooking something. – Martin R Jun 21 '13 at 12:23
  • @MartinR The rects used to position the labels all look different to me, but I see how this could all be put into the cell init method now. – trojanfoe Jun 21 '13 at 12:25
  • @trojanfoe: One cell has 5 labels (with different positions of course). But that are the same positions for each cell created, so the entire code for creating a single cell can be put in the subclass initXXX method. – Martin R Jun 21 '13 at 12:28

5 Answers5

10

Here is the basic code for a subclass of UITableCellView :

#import <UIKit/UIKit.h>

@interface CustomCell : UITableViewCell
{
}
@end


-----------------------------------------------------------


#import "CustomCell.h"

@implementation CustomCell


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
}

/*
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}*/

@end

It is auto-generated if you create a new file of type Objective-C Class and specify UITableViewCell in filed subclass of

zbMax
  • 2,756
  • 3
  • 21
  • 42
3

Following is what I usually do. If you use the cell only in 1 view controller, you can just put it in the same file as the view controller.

@interface MyCell : UITableViewCell

@property (strong, nonatomic) UILabel* nameValue;
@property (strong, nonatomic) UILabel* colorValue;
@property (strong, nonatomic) UILabel* priceValue;

@end

@implementation MyCell

-(id)init {
    self = [super initWithStyle:whatever_style];

    // Create & position UI elements
    UILabel* nameLabel = [[UILabel alloc] init];
    nameLabel.frame = .... // frame, font, etc
    [self.contentView addSubview:nameLabel]

    self.nameValue = [[UILabel alloc] init];
    self.nameValue = .... // frame, font, etc
    [self.contentView addSubview:self.nameValue];

    // Do the same thing for color, price

    return self;
}

@end

By exposing nameValue, colorValue, priceValue, I allow them to be changed from outside (ie the UITableViewController). I didn't expose other labels because they are static. Unless you need special positioning, you don't have to override layoutSubviews. autoresizingMask is sufficient in most cases.

Khanh Nguyen
  • 11,112
  • 10
  • 52
  • 65
1

There is two way I use to solve this.

The "quick and dirty" is to design a UITableViewCell into your UITableView with the stuff you need (UILabel, UIImageView,...) and set a unique tag for each element, then when you dequeue a UITableViewCell you can reuse the elements in like this :

UILabel *nameLabel = (UILabel*)[cell viewWithTag:NAME_LABEL_TAG];

if(!nameLabel) {

    // If the label does not exist, create it
    CGRect nameLabelRect = CGRectMake(0, 5, 70, 20);
    nameLabel = [[UILabel alloc] initWithFrame:nameLabelRect];
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.text = @"Name";
    nameLabel.font = [UIFont boldSystemFontOfSize:12];
    [cell.contentView addSubview: nameLabel];
}

Or the (imo) best way is to create a custom UITableViewCell and subclass UItableviewCell, you have a good tutorial there : Custom UITableViewCell

Edelweiss
  • 621
  • 1
  • 9
  • 24
0

I guess your are putting this stuff in your cellForRowAtIndexPath: delegate method and I can see why your strive to remove it from this place.

Create a new Objective-C class via New->File and put the subview related calls you posted in the layoutSubviews: method. In cellForRowAtIndexPath: in your table view delegate now use this class instead of the generic UITableViewCell. Don't forget to import your newly created file.

ff10
  • 3,046
  • 1
  • 32
  • 55
0
#import "CellVideo.h"

@implementation CellVideo

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

    return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
    NSLog(@"initWithCoder");
    self = [super initWithCoder: aDecoder];
    if (self)
    {
        // Initialization code


        MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] init];
        [moviePlayer.view setFrame:CGRectMake(10, 75, 300, 260)];
        [moviePlayer.view setBackgroundColor:[UIColor blackColor]];
        [moviePlayer.view setTag:333];
        [moviePlayer setControlStyle:MPMovieControlStyleNone];
         moviePlayer.scalingMode = MPMovieScalingModeFill;
        _movie=moviePlayer;

        UIImageView *imagrViewThumb=[[UIImageView alloc]initWithFrame:CGRectMake(10, 75, 300, 260)];
        [imagrViewThumb setBackgroundColor:[UIColor redColor]];
        [imagrViewThumb setTag:333];

        [self.contentView insertSubview:imagrViewThumb atIndex:0];
    }
    return self;
}
-(void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}


///use it in this way
 CellIdentifier=@"cellvideo";
UITableViewCell *cell=nil;
//  CellVideo *cellVideo=nil;

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Sishu
  • 1,510
  • 1
  • 21
  • 48