3

我有这个代码段:

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 它会在子类中完成所有事情。

4

5 回答 5

10

这是 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

于 2013-06-21T12:23:22.470 回答
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

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

于 2013-06-21T13:05:22.013 回答
1

我有两种方法可以解决这个问题。

“快速而肮脏”是用你需要的东西(, ,...)设计 aUITableViewCellUITableViewUILabel每个UIImageView元素设置一个唯一的标签,然后当你出列 a 时,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,你有一个很好的教程:自定义 UITableViewCell

于 2013-06-21T12:33:21.353 回答
0

我猜你正在把这些东西放在你的 cellForRowAtIndexPath: 委托方法中,我明白你为什么要努力从这个地方删除它。

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

于 2013-06-21T12:26:15.267 回答
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];
}
于 2014-08-26T09:47:23.017 回答