iOS学习 UITableViewCell 三种定制方法

来源:互联网 发布:linux 浏览网页 编辑:程序博客网 时间:2024/06/05 02:37

@implementation HomeTableViewController


- (void)viewDidLoad {

    [superviewDidLoad];

}

#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 = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier: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 = [[DetailTableViewControlleralloc]init];

    //记录选中的内容

    detailTVC.cellType = indexPath.row;

    [self.navigationControllerpushViewController: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 {

    [superviewDidLoad];

    

    _listArray = [UIFontfamilyNames];

    self.tableView.rowHeight =60 ;    

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // 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 = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIdentifier];

            //第一种定制方法

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

            label.backgroundColor = [UIColorcyanColor];

            label.tag = 101;

            [cell.contentView addSubview:label];

        }

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

        label.text = _listArray[indexPath.row];

        

        return cell;

    } else if (self.cellType ==kNibCellType )

    {

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (cell == nil) {

            NSArray *nibs = [[NSBundlemainBundle]loadNibNamed:@"MyView"owner:selfoptions:nil];

            cell = [nibs objectAtIndex:0];

        }

        //第二种定制方法

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

        label.text = _listArray[indexPath.row];

        

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

        imgView.backgroundColor = indexPath.row %2 ==0 ?[UIColoryellowColor]:[UIColorredColor];

        

        return cell;

    }else if (self.cellType ==kCustomCellType )

    {

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

        MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (cell == nil) {

            cell = [[MyCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier: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 = [superinitWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {

        [self initSubViews]; //调用方法

    }

    return self;

}


//定制Cell内容

- (void)initSubViews{

    _label = [[UILabelalloc]initWithFrame:CGRectZero];

    _label.backgroundColor = [UIColorcyanColor];

    [self.contentViewaddSubview:_label];

}


//改变系统样式位置

- (void)layoutSubviews{

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


    //修改label位置

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

    

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

    _label.text =self.text;    

}





0 0