UITableViewCell子类

时间:2013-06-21 12:11:17

标签: ios objective-c uitableview subclass

我有这段代码:

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

我想把它变成一个子类,所以我不必编写所有这些行,我只是说cell = CustomCell并且它在子类中完成所有操作。

5 个答案:

答案 0 :(得分:9)

以下是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

如果您创建Objective-C Class类型的新文件并在归档UITableViewCell中指定subclass of,则会自动生成

答案 1 :(得分:3)

以下是我通常做的事情。如果仅在1个视图控制器中使用单元,则可以将其放在与视图控制器相同的文件中。

@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

通过公开nameValuecolorValuepriceValue,我允许从外部更改它们(即UITableViewController)。我没有暴露其他标签,因为它们是静态的。除非您需要特殊定位,否则不必覆盖layoutSubviews。在大多数情况下,autoresizingMask就足够了。

答案 2 :(得分:1)

我用两种方法来解决这个问题。

“快速而肮脏”是在UITableViewCell中设置一个UITableView,其中包含您需要的内容(UILabelUIImageView,...)并设置每个元素的唯一标记,然后当您将UITableViewCell出列时,您可以重复使用这样的元素:

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

或者(imo)最好的方法是创建自定义UITableViewCell和子类UItableviewCell,你有一个很好的教程:Custom UITableViewCell

答案 3 :(得分:0)

我想你正在把这些东西放在你的cellForRowAtIndexPath:委托方法中,我可以看到为什么你努力将它从这个地方删除。

通过New-&gt; File创建一个新的Objective-C类,并将您发布的子视图相关调用放在layoutSubviews:方法中。在cellForRowAtIndexPath中:在表视图中,委托现在使用此类而不是通用的UITableViewCell。不要忘记导入新创建的文件。

答案 4 :(得分: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];
}
相关问题