iOS 中自定义TableViewCell方法

自定义cell时需要继承UITableViewCell.    

举例:ZLSchoolListTableCell继承UITableViewCell

ZLSchoolListTableCell.h文件

#import <UIKit/UIKit.h>

@class SchoolModel(模型);

@interface ZLSchoolListTableCell : UITableViewCell

+(instancetype)SchoolListWithTableView:(UITableView*)tableView;

@property(nonatomic,strong)SchoolModel *Zlschool;(模型属性)

@end

ZLSchoolListTableCell.m文件

#import "ZLSchoolListTableCell.h"

#import "School.h"

@interface ZLSchoolListTableCell()

/** 图标 */

@property (nonatomic, weak) UIImageView *iconView;

/** 中文名 */

@property (nonatomic, weak) UILabel *ChineseLabel;

 

@end

@implementation ZLSchoolListTableCell

 

+ (instancetype)SchoolListWithTableView:(UITableView *)tableView

{

    static NSString *ID = @"ZLSchoolListTableCell";

    ZLSchoolListTableCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    if (cell == nil) {

        cell = [[ZLSchoolListTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];

    }

    return cell;

}

 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {

        [self setSubwiews];

        

    }

    return self;

}

- (void)setSubwiews{

   /** 图标 */

    UIImageView *iconView = [[UIImageView alloc] init];

    iconView.userInteractionEnabled = YES;

    [self.contentView addSubview:iconView];

    self.iconView = iconView;

    

   /** 中文名 */

    UILabel *nameLabel = [[UILabel alloc] init];

    nameLabel.textColor = IWTextColorTitle;

    nameLabel.font = IWTextFont16;

    [self.contentView addSubview:nameLabel];

    self.ChineseLabel = nameLabel;

}

- (void)setZlschool:(School *)Zlschool{

    _Zlschool = Zlschool;

    [self settingData];(设置数据)

    [self setSubwiewsFrame];(设置frame)

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

        ZLSchoolListTableCell *cell = [ZLSchoolListTableCell SchoolListWithTableView:tableView];

        SchoolModel*school =self.SChoolArry[indexPath.row];

        cell.Zlschool =school;

        return cell;

}

posted @ 2016-08-18 18:36  瀚海枭龙  阅读(754)  评论(0编辑  收藏  举报