单元格的三种定制方式

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];
}
时间: 2024-10-05 06:03:54

单元格的三种定制方式的相关文章

试比较三种交换方式

参考链接:http://blog.chinaunix.net/uid-21411227-id-1826932.html三种交换技术及其比较2010-09-22 17:17:57 分类: 系统运维 一.电路交换: “电路交换”(Circuit Switching)又称为“线路交换”,是一种面向连接的服务.两台计算机通过通信子网进行数据电路交换之前,首先要在通信子网中建立一个实际的物理线路连接.最普通的电路交换例子是电话系统.电路交换是根据交换机结构原理实现数据交换的.其主要任务是把要求通信的输入端

单例模式的三种实现方式

一.单例模式的三种实现方式 1. 什么是单例模式 基于某种方法,实例化多次,得到同一个实例/对象 2. 为什么用单例模式 实例化多次,得到的对象属性内容都一样时,应该将这些对象指向同一个内存,即同一个实例,来节省内存空间 1. 实现单例模式方式一:类内部定义类方法实现 实现方法:类中定义了一个类方法 # 未单例模式前 import setting class Mysql: def __init__(self,ip,port): self.ip=ip self.port=port @classme

CSS的三种定位方式介绍(转载)

在CSS中一共有N种定位方式,其中,static ,relative,absolute三种方式是最基本最常用的三种定位方式.他们的基 本介绍如下. static默认定位方式relative相对定位,相对于原来的位置,但是原来的位置仍然保留absolute定位,相对于最近的非标准刘定位,原来的位置消失,被后边的位置所顶替 下面先演示相对定位的案例 [html] view plain copyprint? <!DOCTYPE html> <html> <head> <

MyEclipse中web服务器的三种配置方式

初学Javaweb开发的人们都会遇到一个问题,就是服务器环境的搭建配置问题.下面介绍三种服务器的搭建方式. 直接修改server.xml文件 当你写了一个web应用程序(jsp/servlet),想通过浏览器直接去访问这个页面,需要在Tomcat中配置相关路径: 找到Tomcat下conf目录,你会看到有个server.xml,即服务器配置文件.用文本编译器打开,拉到Host标签,在它结束前加上我们的应用程序路径: <Context path="/HelloWeb" docBas

1、打印二进制机器码,程序内存分析,大端序小端序,指针数组,数组指针,数组的三种访问方式,typedef,#if-0-#endif,求数组大小,括号表达式

 1.打印二进制机器码(分别表示32位的和64位的) #include <stdio.h> /*按照8位的长度打印一个数值*/ void dis8bit(char val) { int bit = 8; while(bit--) { if(1<<bit&val){ printf("1"); } else { printf("0"); } if(!(bit%4)) printf(" "); } putchar(1

SQL Server 中的三种分页方式

USE tempdb GO SET NOCOUNT ON --创建表结构 IF OBJECT_ID(N'ClassB', N'U') IS NOT NULL DROP TABLE ClassB GO CREATE TABLE ClassB(ID INT PRIMARY KEY, Name VARCHAR(16), CreateDate DATETIME, AID INT, Status INT) CREATE INDEX IDX_CreateDate ON ClassB(CreateDate)

支付宝5月4日起将停止收款主页业务 保留三种收款方式

4月28日消息,支付宝近日发布公告称,将于5月4日起停止收款主页业务(产品功能将无法使用),但并未公布停止业务的具体信息. 据了解,收款主页业务是用户可以自己制作一个支付宝账号的链接,把这个链接发给付款人后,对方就可以输入金额给该用户付款. 支付宝收款主页截图 支付宝公告称,收款主页业务停止之后,用户可以有三种方式进行收款:生成专属支付宝收款账户码,将账户二维码图片分享出去:在电脑上使用我要收款:在手机上,使用支付宝钱包的我要收款. 以下为公告原文: 鉴于收款主页(https://me.alip

Hibernate的Api以及三种查询方式

Hibernate  Api |-- Configuration       配置管理类对象 config.configure();    加载主配置文件的方法(hibernate.cfg.xml) 默认加载src/hibernate.cfg.xml config.configure("cn/config/hibernate.cfg.xml");   加载指定路径下指定名称的主配置文件 config.buildSessionFactory();   创建session的工厂对象 |--

使用JavaScript判断图片是否加载完成的三种实现方式

有时需要获取图片的尺寸,这需要在图片加载完成以后才可以.有三种方式实现,下面一一介绍. 一.load事件 <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>img - load event</title> </head> <body> <img id="img1" src="http:/