TableView与TableViewCell

鱼征
2023-12-01

TableViewCell

TableViewCell是现实表格的单元格式
基本流程为在init中添加各种可用控件,再编写输入数据的方法,并且依据各数据来设定各控件的位置以及行高


#import "TableViewCell.h"
#import "Weibo.h"

#define NJNameFont [UIFont systemFontOfSize:15]
#define NJTextFont [UIFont systemFontOfSize:16]

@implementation TableViewCell

+ (instancetype) cellWithTableView:(UITableView *) tableView;
{
    static NSString *identifier = @"status";
    //缓存中取
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    //创建
    if(cell == nil)
    {
        cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    return cell;
}

- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        //自定义各种控件
        //定义头像控件
        UIImageView *iconView = [[UIImageView alloc] init];
        [self.contentView addSubview:iconView];
        self.iconView = iconView;
        //自定义昵称
        UILabel *nameLabel = [[UILabel alloc] init];
        [self.contentView addSubview:nameLabel];
        self.nameLabel = nameLabel;
        //创建vip
        UIImageView *vipView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"VIP_32px_534386_easyicon.net.png"]];
        [self.contentView addSubview: vipView];
        self.vipView = vipView;
        //创建正文
        UILabel *introLabel = [[UILabel alloc] init];
        introLabel.numberOfLines = 0;
        [self.contentView addSubview:introLabel];
        self.introLabel = introLabel;
        //创建配图
        UIImageView *pictureView = [[UIImageView alloc] init];
        [self.contentView addSubview:pictureView];
        self.pictureView = pictureView;
    }
    return self;
}


- (void) settingData:(Weibo*) lyzWeibo
{
    //设置头像图片
    CGFloat padding = 10;
    self.iconView.image = [UIImage imageNamed:lyzWeibo.icon];
    CGRect iconF = CGRectMake(10, 10, 30, 30);
    self.iconView.frame = iconF;
    //设置微博名
    self.nameLabel.text = lyzWeibo.name;
    self.nameLabel.font = NJNameFont;
    CGFloat nameLabelX = CGRectGetMaxX(iconF) + padding;
    CGSize nameSize = [self sizeWithString:lyzWeibo.name font:NJNameFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    CGFloat nameLabelY = 10 + (30 - nameSize.height) * 0.5;
    CGRect nameF = CGRectMake(nameLabelX, nameLabelY, nameSize.width, nameSize.height);
    self.nameLabel.frame = nameF;
    //设置vip
    if(lyzWeibo.vip)
    {
        self.vipView.hidden = NO;
        self.nameLabel.textColor = [UIColor redColor];
        CGFloat vipVIewX = CGRectGetMaxX(nameF) + padding;
        CGFloat vipViewY = nameLabelY;
        self.vipView.frame = CGRectMake(vipVIewX, vipViewY, 14, 14);
    }else
    {
        self.vipView.hidden = YES;
    }
    self.cellHeight = CGRectGetMaxY(iconF) + padding;
    //设置正文,if的条件为参数存在且不为空
    if(lyzWeibo.text && (![lyzWeibo.text isEqual: @""]))
    {
        self.introLabel.text = lyzWeibo.text;
        self.introLabel.font = NJTextFont;
        CGFloat introLabelX = 10;
        CGFloat introLabelY = CGRectGetMaxY(iconF) + padding;
        CGSize introSize = [self sizeWithString:lyzWeibo.text font:NJTextFont maxSize:CGSizeMake(MAXFLOAT,MAXFLOAT)];
        CGRect introF = CGRectMake(introLabelX, introLabelY, introSize.width, introSize.height);
        self.introLabel.frame = introF;
        self.cellHeight = CGRectGetMaxY(self.introLabel.frame) + padding;

    }else
    {
        self.introLabel.hidden = YES;
    }
    //设置配图
    if(lyzWeibo.picture && (![lyzWeibo.picture isEqual: @""]))
    {
        self.pictureView.image = [UIImage imageNamed:lyzWeibo.picture];
        if(lyzWeibo.text && (![lyzWeibo.text isEqual: @""]))
        {
            CGFloat pictureX = 10;
            CGFloat pictureY = CGRectGetMaxY(self.introLabel.frame) + padding;
            self.pictureView.frame = CGRectMake(pictureX, pictureY, 100, 100);
        }else
        {
            CGFloat pictureX = 10;
            CGFloat pictureY = CGRectGetMaxY(iconF) + padding;
            self.pictureView.frame = CGRectMake(pictureX, pictureY, 100, 100);
        }
        self.cellHeight = CGRectGetMaxY(self.pictureView.frame) + padding;
    }else
    {
        self.pictureView.hidden = YES;
    }
}

- (CGSize) sizeWithString:(NSString *)str font:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *dict = @{NSFontAttributeName:font};
    CGSize size = [str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
    return size;
}
@end

TableView

在 - (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法中利用TableViewCell的settingData方法来设定TableViewCell并返回,另外需要注意的就是设置行高以及行数,至于数据结构,可另写class,亦可在本Controller中添加@property来获取文件内容进行设置

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger rowNo = indexPath.row;
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier"];

    cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseIdentifier"];
    [cell settingData:[self.statusFrames objectAtIndex:rowNo]];
    // Configure the cell...

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger rowNo = indexPath.row;
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier"];

    cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseIdentifier"];
    [cell settingData:[self.statusFrames objectAtIndex:rowNo]];
    return cell.cellHeight;
}

//statusFrames为数据属性,通过文件获取数据
- (NSArray *) statusFrames
{
    if(_statusFrames == nil)
    {
        NSString * fullPath = [[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil];
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:fullPath];
        NSMutableArray *models = [NSMutableArray arrayWithCapacity:dictArray.count];
        for(NSDictionary *dict in dictArray)
        {
            Weibo *weibo01 = [Weibo weiboWithDict:dict];
            [models addObject:weibo01];

        }
        self.statusFrames = [models copy];
    }
    return _statusFrames;
}
 类似资料: