iOS学习 UITableViewCell 三种定制方法

@implementation HomeTableViewController


- (void)viewDidLoad {

    [super viewDidLoad];

}

#pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 4;

}


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

    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    

    switch (indexPath.row) {

        case 0:

            cell.textLabel.text = @"default Cell";

            break;

        case 1:

            cell.textLabel.text = @"1  concentView";

            break;

        case 2:

            cell.textLabel.text = @"2  Nib";

            break;

        case 3:

            cell.textLabel.text = @"3  custom Cell";

            break;

        default:

            break;

    }    

    return cell;

}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    DetailTableViewController *detailTVC = [[DetailTableViewController alloc]init];

    //记录选中的内容

    detailTVC.cellType = indexPath.row;

    [self.navigationController pushViewController:detailTVC animated:YES];

}




#import <UIKit/UIKit.h>

#import "HomeTableViewController.h"

#import "MyCell.h"


//添加玫举

typedef enum kTableViewCellType{

    kDefualtCellType = 0,

    kConcentCellType = 1,

    kNibCellType     = 2,

    kCustomCellType  = 3

    

} kTableViewCellType;


@interface DetailTableViewController : UITableViewController

{

@private

    NSArray *_listArray;

}


@property (nonatomic,assign) UITableViewCellStyle cellType;


@end





@implementation DetailTableViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    _listArray = [UIFont familyNames];

    self.tableView.rowHeight = 60 ;    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


#pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 20;

}


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

    static NSString *cellIdentifier = @"cell";

    

    if (self.cellType == kConcentCellType )

    {

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (cell == nil) {

            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

            //第一种定制方法

            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, 200, 30)];

            label.backgroundColor = [UIColor cyanColor];

            label.tag = 101;

            [cell.contentView addSubview:label];

        }

        UILabel *label = (UILabel *)[cell.contentView viewWithTag:101];

        label.text = _listArray[indexPath.row];

        

        return cell;

    } else if (self.cellType == kNibCellType )

    {

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (cell == nil) {

            NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"MyView" owner:self options:nil];

            cell = [nibs objectAtIndex:0];

        }

        //第二种定制方法

        UILabel *label = (UILabel *)[cell.contentView viewWithTag:201];

        label.text = _listArray[indexPath.row];

        

        UIImageView *imgView = (UIImageView *)[cell.contentView viewWithTag:202];

        imgView.backgroundColor = indexPath.row %2 == 0 ?[UIColor yellowColor]:[UIColor redColor];

        

        return cell;

    }else if (self.cellType == kCustomCellType )

    {

        //第三种定制方法,单独创建cell

        MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (cell == nil) {

            cell = [[MyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

        }

        //MyCell类中的text赋值

        cell.text = _listArray[indexPath.row];

        

        return cell;

    }else{

        return nil;

    }

}





#import <UIKit/UIKit.h>


@interface MyCell : UITableViewCell

{

@private

    UILabel *_label;

}


@property (nonatomic,copy) NSString *text;


@end





#import "MyCell.h"

//声明私有方法,方便阅读

@interface MyCell()


- (void)initSubViews;


@end


@implementation MyCell


- (void)awakeFromNib {

    // Initialization code

}


//重写init方法

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

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

    if (self) {

        [self initSubViews];  //调用方法

    }

    return self;

}


//定制Cell内容

- (void)initSubViews{

    _label = [[UILabel alloc]initWithFrame:CGRectZero];

    _label.backgroundColor = [UIColor cyanColor];

    [self.contentView addSubview:_label];

}


//改变系统样式位置

- (void)layoutSubviews{

    [super layoutSubviews];  //一定要调用父类,否则无法排版


    //修改label位置

    _label.frame = CGRectMake(80, 10, 200, 40);

    

    //label引用赋值,注意位置

    _label.text = self.text;    

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值