码迷,mamicode.com
首页 > 其他好文 > 详细

单元格的三种定制方式

时间:2014-10-30 11:50:20      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:mvc   xib   单元格定制   系统内置   

AppDelegate.m

MainViewController *mainCtrl = [[MainViewController alloc] initWithStyle:UITableViewStylePlain];
    
    UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:mainCtrl];
    
    self.window.rootViewController = navCtrl;
    
    [mainCtrl release];
    [navCtrl release];

MainViewController.m

#import "MainViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        self.title = @"单元格定制方式";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //设置视图回来以后是否存在选中效果,默认是yes
//    self.clearsSelectionOnViewWillAppear = NO;
    
}

- (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 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *iden = @"cell_Root";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"第%d中单元格定制方式",indexPath.row+1];
    
    return cell;
}

//点击单元格相应的协议方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    //点击第一个单元格相应事件
    if (indexPath.row == 0) {
        FirstViewController *firstCtrl = [[FirstViewController alloc] init];
        
        [self.navigationController pushViewController:firstCtrl animated:YES];
        
        [firstCtrl release];
    }else if (indexPath.row == 1) {
    
        SecondViewController *secondCtrl = [[SecondViewController alloc] init];
        
        [self.navigationController pushViewController:secondCtrl animated:YES];
        
        [secondCtrl release];
        
    }else if (indexPath.row == 2) {
    
        ThirdViewController *thirCtrl = [[ThirdViewController alloc] init];
        
        [self.navigationController pushViewController:thirCtrl animated:YES];
        
        [thirCtrl release];
    }
    
}


@end
----------------------------第一种制定方式[系统内置]-------------------------------

FirstViewController.h

@interface FirstViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain)NSArray *data;

FirstViewController.m

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"第一种单元格定制方式";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    //创建表视图
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 460) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    [tableView release];
    
    //读取文件
    NSString *path = [[NSBundle mainBundle] pathForResource:@"news" ofType:@"plist"];
    _data = [[NSArray alloc] initWithContentsOfFile:path];
}

#pragma mark - UITableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _data.count;
    
}

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

    static NSString *iden = @"Cell_first";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden] autorelease];
        
        //给cell添加子视图
        //1.创建显示标题的label
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 300, 20)];
        titleLabel.tag = 101;
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.font = [UIFont boldSystemFontOfSize:15];
        [cell.contentView addSubview:titleLabel];
        [titleLabel release];
        //2.创建显示评论数的label
        UILabel *commentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 25, 150, 20)];
        commentLabel.font = [UIFont systemFontOfSize:13];
        commentLabel.tag = 102;
        commentLabel.textColor = [UIColor grayColor];
        commentLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:commentLabel];
        [commentLabel release];
        //3.创建显示发表时间的label
        UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(210, 25, 100, 20)];
        timeLabel.font = [UIFont systemFontOfSize:13];
        timeLabel.tag = 103;
        timeLabel.textColor = [UIColor grayColor];
        timeLabel.textAlignment = NSTextAlignmentRight;
        timeLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:timeLabel];
        [timeLabel release];
    }
    
    //添加数据
    
    NSDictionary *dic = [_data objectAtIndex:indexPath.row];
    //评论时间
    NSString *time = [dic objectForKey:@"time"];
    //评论数
    NSString *count = [dic objectForKey:@"commentCount"];
    //标题
    NSString *title = [dic objectForKey:@"title"];
    
    UILabel *titleLabel = (UILabel *)[cell.contentView viewWithTag:101];
    titleLabel.text = title;
    UILabel *commentLabel = (UILabel *)[cell.contentView viewWithTag:102];
    commentLabel.text = [NSString stringWithFormat:@"%@条评论数",count];
    UILabel *timeLabel = (UILabel *)[cell.contentView viewWithTag:103];
    timeLabel.text = [NSString stringWithFormat:@"%@小时前",time];
    
    
    return cell;
}
-----------------------------第二种制定方式[Nib文件]-------------------------------
SecondViewController.h

@interface SecondViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain)NSArray *data;
SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"单元格第二种定制方式";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    //创建表视图
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 460) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    [tableView release];
    
    //读取文件
    NSString *path = [[NSBundle mainBundle] pathForResource:@"news" ofType:@"plist"];
    _data = [[NSArray alloc] initWithContentsOfFile:path];
}

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

    return _data.count;
    
}

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

    static NSString *iden = @"cell_second";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil] lastObject];
    }
    
    //添加数据
    
    NSDictionary *dic = [_data objectAtIndex:indexPath.row];
    //评论时间
    NSString *time = [dic objectForKey:@"time"];
    //评论数
    NSString *count = [dic objectForKey:@"commentCount"];
    //标题
    NSString *title = [dic objectForKey:@"title"];
    
    UILabel *titleLabel = (UILabel *)[cell.contentView viewWithTag:101];
    titleLabel.text = title;
    UILabel *commentLabel = (UILabel *)[cell.contentView viewWithTag:102];
    commentLabel.text = [NSString stringWithFormat:@"%@条评论数",count];
    UILabel *timeLabel = (UILabel *)[cell.contentView viewWithTag:103];
    timeLabel.text = [NSString stringWithFormat:@"%@小时前",time];
    
    return cell;
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
------------------------------------------第三种制定方式[自定义单元格]----------------------------
ThirdViewController.h

@interface ThirdViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain)NSMutableArray *data;
ThirdViewController.m

#import "ThirdViewController.h"
#import "NewsModel.h"
#import "NewsCell.h"

@interface ThirdViewController ()

@end

@implementation ThirdViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"单元格第三种定制方式";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    //加载数据
    NSString *path = [[NSBundle mainBundle] pathForResource:@"news" ofType:@"plist"];
    NSArray *arrary = [[NSArray alloc] initWithContentsOfFile:path];
    
    _data = [[NSMutableArray alloc] init];
    
    //将字典中的数据交给news对象
    for (NSDictionary *dic in arrary) {
        NewsModel *model = [[NewsModel alloc] init];
//        NSString *title = [dic objectForKey:@"title"];
//        model.title = title;
        model.title = [dic objectForKey:@"title"];
        model.commentCount = [dic objectForKey:@"commentCount"];
        model.time = [dic objectForKey:@"time"];
        
        //将model对象存放到数组中
        [_data addObject:model];
        [model release];
    }
    
    //创建表视图
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 460) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    
    
}

#pragma mark - UITableView dataSource

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

    return _data.count;
    
}

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

    static NSString *iden = @"cell_Three";
    
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    
    if (cell == nil) {
        cell = [[[NewsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden] autorelease];
    }
    
    cell.model = [_data objectAtIndex:indexPath.row];
    
    return cell;
    
}

@end
NewsCell.h

@class NewsModel;

@interface NewsCell : UITableViewCell {

    UILabel *_titleLabel;
    UILabel *_commentLabel;
    UILabel *_timeLablel;
    
}

@property(nonatomic, retain)NewsModel *model;

NewsCell.m

#import "NewsCell.h"
#import "NewsModel.h"

@implementation NewsCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self _initView];
    }
    return self;
}

- (void)_initView {

    //创建显示标题的label
//    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 300, 20)];
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [self.contentView addSubview:_titleLabel];
    
    //创建显示评论数的label
//    _commentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 25, 150, 20)];
    _commentLabel = [[UILabel alloc] init];
    _commentLabel.backgroundColor = [UIColor clearColor];
    _commentLabel.textColor = [UIColor grayColor];
    _commentLabel.font = [UIFont boldSystemFontOfSize:13];
    [self.contentView addSubview:_commentLabel];

    //创建显示发表时间的label
//    _timeLablel  = [[UILabel alloc] initWithFrame:CGRectMake(200, 25, 100, 20)];
    _timeLablel  = [[UILabel alloc] init];
    _timeLablel.backgroundColor = [UIColor clearColor];
    _timeLablel.textColor = [UIColor grayColor];
    _timeLablel.font = [UIFont systemFontOfSize:13];
    [self.contentView addSubview:_timeLablel];
    
}

/*
 视图将要显示的时候调用
 */
/*
 1.布局,设置frame
 2.添加数据
 */
- (void)layoutSubviews {

    [super layoutSubviews];
    //布局
    _titleLabel.frame = CGRectMake(10, 5, 300, 20);
    _commentLabel.frame = CGRectMake(10, 25, 150, 20);
    _timeLablel.frame = CGRectMake(200, 25, 100, 20);
    
    //添加数据
    _titleLabel.text = _model.title;
    _commentLabel.text = [NSString stringWithFormat:@"评论数%@条",_model.commentCount];
    _timeLablel.text = [NSString stringWithFormat:@"%@小时前",_model.time];
}

@end

NewsModel.h

@interface NewsModel : NSObject

@property(nonatomic, copy)NSString *title;  //标题
@property(nonatomic, copy)NSString *commentCount;   //评论数
@property(nonatomic, copy)NSString *time;   //时间

NewsModel.m

- (void)dealloc
{
    [_commentCount release];
    [_title release];
    [_time release];
    
    [super dealloc];
}







单元格的三种定制方式

标签:mvc   xib   单元格定制   系统内置   

原文地址:http://blog.csdn.net/pengyuan_d/article/details/40616145

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!