本文介绍了UITableViewCell 子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码段:

if (cell == nil){CGRect cellFrame = CGRectMake(0,0,300,250);cell = [[UITableViewCell alloc] initWithFrame:cellFrame重用标识符:CellTableIndetifier];CGRect nameLabelRect = CGRectMake(0, 5, 70, 20);UILabel* nameLabel = [[UILabel alloc] initWithFrame:nameLabelRect];nameLabel.textAlignment = NSTextAlignmentCenter;nameLabel.text = @"姓名";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 = @"颜色";colorLabel.font = [UIFont boldSystemFontOfSize:12];[cell.contentView addSubview: colorLabel];CGRect priceLabelRect = CGRectMake(0, 45, 70, 20);UILabel *priceLabel = [[UILabel alloc] initWithFrame:priceLabelRect];priceLabel.text = @"价格";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 @interface CustomCell : UITableViewCell{}@结尾-----------------------------------------------------------#import "CustomCell.h"@实现自定义单元格- (id)initWithStyle:(UITableViewCellStyle)style repeatIdentifier:(NSString *)reuseIdentifier{self = [super initWithStyle:style 重用标识符:reuseIdentifier];如果(自我){//初始化代码}回归自我;}-(void)layoutSubviews{[超级布局子视图];}/*- (void)setSelected:(BOOL)selected 动画:(BOOL)动画{[super setSelected:selectedanimated:animated];//为选中状态配置视图}*/@结尾

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

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.

解决方案

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

这篇关于UITableViewCell 子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 04:54