[TwistedFate]UITableViewCell自定義-01

自定義cell

步驟:

  1. 創建TableViewCell的子類
  2. 重寫初始化方法
  3. 要添加的控件添加到到cell的現實內容區域contentView上面
  4. 把系統的cell 替換成自定義cell 完成

創建MyTableViewCell類

//  自定義初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {

        [self addSubViews];

    }

    return self;

}

//  添加子視圖
- (void)addSubViews{

    self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(kMargin, kMargin, kImageWidth, kImageHeight)];

    self.imageV.backgroundColor = [UIColor redColor];

    [self.contentView addSubview:self.imageV];
    [_imageV release];

    self.nameLable = [[UILabel alloc] initWithFrame:CGRectMake(self.imageV.XWidth + kMargin, self.imageV.Y - 2.5, kScreenWidth - self.imageV.XWidth - 2 * kMargin, kImageHeight / 3  )];

    self.nameLable.backgroundColor = [UIColor cyanColor];

    [self.contentView addSubview:self.nameLable];

    self.phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.nameLable.X, self.nameLable.YHeight + 5, kScreenWidth - self.imageV.XWidth - 2 * kMargin, kImageHeight / 3  )];

    self.phoneLabel.backgroundColor = [UIColor cyanColor];

    [self.contentView addSubview:self.phoneLabel];

    self.genderLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.phoneLabel.X, self.phoneLabel.YHeight + 5, kScreenWidth - self.imageV.XWidth - 2 *kMargin, kImageHeight / 3 )];

    self.genderLabel.backgroundColor = [UIColor cyanColor];

    [self.contentView addSubview:self.genderLabel];

}

封裝以model類傳值

在model類自定義的cell類中聲明一個model類屬性

@property (nonatomic, retain) Student *stuModel;
- (void)setStuModel:(Student *)stuModel;

重寫model屬性的set方法

//  重寫model的set方法 使賦值model的同時也給控件賦值
- (void)setStuModel:(Student *)stuModel{


    if (_stuModel != stuModel) {

        [_stuModel release];

        _stuModel = [stuModel retain];

    }

    self.nameLable.text = stuModel.name;
    self.phoneLabel.text = stuModel.gender;
    self.genderLabel.text = stuModel.phoneNumber;
    self.imageV.image = [UIImage imageNamed:@"拔刀齋.jpg"];

}

返回cell

static NSString *identifier = @"MaleCell";
        MaleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

        if (cell == nil) {

            cell = [[[MaleTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier] autorelease];
        }
//  以model直接賦值
    Student *stu = self.dataArray[indexPath.row];
    cell.stuModel = stu;
    return cell;

}
    //在給model賦值時 也給cell的控件完成賦值

返回不同的cell

再創建一個自定義cell類

//  重寫初始化方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

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

    if (self) {

        [self addSubviews];

    }

    return self;

}


- (void)addSubviews{

    self.nameLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width / 4, 40)];
    self.nameLable.backgroundColor = [UIColor redColor];

    [self.contentView addSubview:self.nameLable];

    [_nameLable release];
}

//  重寫model的set方法 使賦值model的同時也給控件賦值
- (void)setStuModel:(Student *)stuModel{


    if (_stuModel != stuModel) {

        [_stuModel release];

        _stuModel = [stuModel retain];

    }

    self.nameLable.text = stuModel.name;

}

根據條件返回不同的cell

//  返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    Student *stu = self.dataArray[indexPath.row];


    //  根據stuModel的值進行判斷選用不一樣的cell顯示
    if ([stu.gender isEqualToString:@"女"]) {

        //  創建女的
        static NSString *identifier = @"GirlCell";

        MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

        if (cell == nil) {

            cell = [[[MyTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier] autorelease];

        }

        cell.stuModel = stu;
        return cell;

    }else{

        //  創建男的
        static NSString *identifier = @"MaleCell";

        MaleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

        if (cell == nil) {

            cell = [[[MaleTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier] autorelease];

        }

        cell.stuModel = stu;
        return cell;

    }

    //  在給model賦值時 也給cell的控件完成賦值

}
發佈了68 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章