UITableViewCell子类

我有这个代码段:

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,它在子类中做了一切。

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

它是自动生成的,如果您创build一个typesObjective-C Class的新文件,并指定UITableViewCell的归档subclass of

以下是我通常做的事情。 如果仅在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是足够的。

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

“快速和肮脏的”是用你需要的东西( UILabelUIImageView ,…)为你的UITableViewdevise一个UITableViewCell ,并为每个元素设置一个唯一的标签,然后当你退出一个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)最好的方法是创build一个自定义的UITableViewCell和子类UItableviewCell ,你有一个很好的教程: 自定义的UITableViewCell

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

通过New-> File创build一个新的Objective-C类,并把你在layoutSubviews:方法中发布的子视图相关调用。 在cellForRowAtIndexPath中:在你的表视图中,委托现在使用这个类来代替通用的UITableViewCell。 不要忘记导入新创build的文件。

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