iOS--xib自定义cell

转载自:http://www.th7.cn/Program/IOS/201407/251523.shtml


最终效果图:

xib+自定义cell的封装标准步骤:(按此步骤者,事半功倍)

 

 

xib+自定义cell的封装标准步骤:(按此步骤者,事半功倍)
 1,新建GirlCell.xib,往里面拖一个tableViewCell,在cell中拖入各种控件
 2,新建GirlCell类,继承自uitableViewCell
 3,将GirlCell.xib界面中cell的类名更改成GirlCell,并且指定重用的identifer,一般写类名
 4,将GirlCell.xib界面中所有的各种控件,<span id="26_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="26_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%C1%AC%CF%DF&k0=%C1%AC%CF%DF&kdi0=0&luki=6&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="26" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">连线</span></a></span>到类GirlCell中,使它们都成为GirlCell类的成员
 5,新建<span id="27_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="27_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%CA%FD%BE%DD%C4%A3%D0%CD&k0=%CA%FD%BE%DD%C4%A3%D0%CD&kdi0=0&luki=8&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="27" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">数据模型</span></a></span> Girl,方法列表如下
        
        // UI控件用weak,字符串用copy,其他对象用strong
        // 头像图片名
        @property(nonatomic,copy)NSString *headImgName;
        // 姓名
        @property(nonatomic,copy)NSString *name;
        // 判词
        @property(nonatomic,copy)NSString *verdict;

        // 类方法,字典 转 对象 类似javaBean一次性<span id="28_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="28_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%CC%EE%B3%E4&k0=%CC%EE%B3%E4&kdi0=0&luki=2&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="28" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">填充</span></a></span>
        + (Girl *)girlWithDict:(NSDictionary *)dict;

        // 对象方法,设置对象的属性后,返回对象
        - (Girl *)initWithDict:(NSDictionary *)dict;


6,封装的GirlCell,方法列表如下
 
         @property (weak, nonatomic) IBOutlet UIImageView *headImg;
         @property (weak, nonatomic) IBOutlet UILabel *name;
         @property (weak, nonatomic) IBOutlet UILabel *verdict;
         
         // 返回xib界面中cell的高度
         + (CGFloat)cellHeight;
         
         // 返回xib界面上写的重用cellID
         + (NSString *)cellID;
         
         // 从xib中加载 实例化一个girlCell对象
         + (GirlCell *)girlCell;
         
         // 参数是<span id="29_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="29_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%CA%FD%BE%DD%C4%A3%D0%CD&k0=%CA%FD%BE%DD%C4%A3%D0%CD&kdi0=0&luki=8&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="29" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">数据模型</span></a></span>girl对象,<span id="30_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="30_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%CC%EE%B3%E4&k0=%CC%EE%B3%E4&kdi0=0&luki=2&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="30" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">填充</span></a></span>值到xib中各个控件 ,并返回封装好数据之后的,girlCell对象
         - (GirlCell *)cellWithGirl:(Girl *)girl;
 
 7,<span id="31_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="31_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%BF%D8%D6%C6%C6%F7&k0=%BF%D8%D6%C6%C6%F7&kdi0=0&luki=10&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="31" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">控制器</span></a></span>中 cellForRow方法如下
     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     {
         // 把cell封装之后,控制 器知道的东西就非常少了~
         
         // 先从缓存池中,根据ID取出自已的cell类
         GirlCell *girlCell = [tableView dequeueReusableCellWithIdentifier:[GirlCell cellID]];
         if (girlCell == nil) {
         // 如果池中没取到,则类方法,重新生成一个崭新的girlCell,xib界面中指定了重用cellID
         girlCell = [GirlCell girlCell];
         }
         // 设置cell中<span id="32_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="32_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%B6%C0%D2%BB%CE%DE%B6%FE&k0=%B6%C0%D2%BB%CE%DE%B6%FE&kdi0=0&luki=1&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="32" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">独一无二</span></a></span>的内容
         Girl *girl = [_girls objectAtIndex:indexPath.row];
         // 参数为girl对象,<span id="33_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="33_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%CC%EE%B3%E4&k0=%CC%EE%B3%E4&kdi0=0&luki=2&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="33" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">填充</span></a></span>其各个成员值到xib中各个控件 ,并返回填充好的cell
         girlCell = [girlCell cellWithGirl:girl];
         // 返回cell
         return girlCell;
     
     
     }


创建tableViewController的标准步骤:

 

 

创建tableViewController的标准步骤:
 1,新建<span id="24_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="24_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%B9%A4%B3%CC&k0=%B9%A4%B3%CC&kdi0=0&luki=3&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="24" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">工程</span></a></span>,然后,删除viewController.h和.m文件
 2,new File 选择OC 类,<span id="25_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="25_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%BC%CC%B3%D0&k0=%BC%CC%B3%D0&kdi0=0&luki=5&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="25" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">继承</span></a></span>自UITableViewController
 3,到main.storyboard中删除 默认的viewController
 4,拖动一个黄色的tableViewController到main.storyboard,并更改file's owner为自已经建立的BeyondTableViewController
 5,仍然到main.storyboard中删除 自动生成的cell控件
 6,运行既可

 

 

 

 

Girl.h

 

 
//
//  Girl.h
//  12_tableView的增删改
//
//  Created by beyond on 14-7-27.
//  Copyright (c) 2014年 com.beyond. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Girl : NSObject
// UI控件用weak,字符串用copy,其他对象用strong
// 头像图片名
@property(nonatomic,copy)NSString *headImgName;
// 姓名
@property(nonatomic,copy)NSString *name;
// 判词
@property(nonatomic,copy)NSString *verdict;



// 类方法,字典 转 对象 类似javaBean一次性<span id="23_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="23_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%CC%EE%B3%E4&k0=%CC%EE%B3%E4&kdi0=0&luki=2&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="23" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">填充</span></a></span>
+ (Girl *)girlWithDict:(NSDictionary *)dict;

// 对象方法,设置对象的属性后,返回对象
- (Girl *)initWithDict:(NSDictionary *)dict;
@end


Girl.m

 

 
//
//  Girl.m
//  12_tableView的增删改
//
//  Created by beyond on 14-7-27.
//  Copyright (c) 2014年 com.beyond. All rights reserved.
//

#import "Girl.h"

@implementation Girl


// 类方法,字典 转 对象 类似javaBean一次性填充
+ (Girl *)girlWithDict:(NSDictionary *)dict
{
    // 只是调用对象的initWithDict方法,之所以用self是为了对子类进行兼容
    return [[self alloc]initWithDict:dict];
}

// 对象方法,设置对象的属性后,返回对象
- (Girl *)initWithDict:(NSDictionary *)dict
{
    // 必须先调用父类NSObject的init方法
    if (self = [super init]) {
        // 设置对象自己的属性
        self.name = dict[@"name"]   ;
        self.headImgName = dict[@"headImg"] ;
        self.verdict = dict[@"verdict"];
    }
    // 返回<span id="22_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="22_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%CC%EE%B3%E4&k0=%CC%EE%B3%E4&kdi0=0&luki=2&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="22" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">填充</span></a></span>好的对象
    return self;
}
@end


GirlCell.xib

GirlCell.h

 

 
//
//  GirlCell.h
//  14_tableViewController_xib自定义cell
//
//  Created by beyond on 14-7-28.
//  Copyright (c) 2014年 com.beyond. All rights reserved.
/*
 xib+自定义cell的封装标准步骤:(按此步骤者,事半功倍)
 1,新建GirlCell.xib,往里面拖一个tableViewCell,在cell中拖入各种控件
 2,新建GirlCell类,<span id="11_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="11_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%BC%CC%B3%D0&k0=%BC%CC%B3%D0&kdi0=0&luki=5&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="11" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">继承</span></a></span>自uitableViewCell
 3,将GirlCell.xib界面中cell的类名更改成GirlCell,并且指定重用的identifer,一般写类名
 4,将GirlCell.xib界面中所有的各种控件,<span id="12_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="12_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%C1%AC%CF%DF&k0=%C1%AC%CF%DF&kdi0=0&luki=6&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="12" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">连线</span></a></span>到类GirlCell中,使它们都成为GirlCell类的成员
 5,新建<span id="13_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="13_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%CA%FD%BE%DD%C4%A3%D0%CD&k0=%CA%FD%BE%DD%C4%A3%D0%CD&kdi0=0&luki=8&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="13" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">数据模型</span></a></span> Girl,方法列表如下
        
        // UI控件用weak,字符串用copy,其他对象用strong
        // 头像图片名
        @property(nonatomic,copy)NSString *headImgName;
        // 姓名
        @property(nonatomic,copy)NSString *name;
        // 判词
        @property(nonatomic,copy)NSString *verdict;

        // 类方法,字典 转 对象 类似javaBean一次性<span id="14_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="14_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%CC%EE%B3%E4&k0=%CC%EE%B3%E4&kdi0=0&luki=2&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="14" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">填充</span></a></span>
        + (Girl *)girlWithDict:(NSDictionary *)dict;

        // 对象方法,设置对象的属性后,返回对象
        - (Girl *)initWithDict:(NSDictionary *)dict;

 
 6,封装的GirlCell,方法列表如下
 
         @property (weak, nonatomic) IBOutlet UIImageView *headImg;
         @property (weak, nonatomic) IBOutlet UILabel *name;
         @property (weak, nonatomic) IBOutlet UILabel *verdict;
         
         // 返回xib界面中cell的高度
         + (CGFloat)cellHeight;
         
         // 返回xib界面上写的重用cellID
         + (NSString *)cellID;
         
         // 从xib中加载 实例化一个girlCell对象
         + (GirlCell *)girlCell;
         
         // 参数是<span id="15_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="15_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%CA%FD%BE%DD%C4%A3%D0%CD&k0=%CA%FD%BE%DD%C4%A3%D0%CD&kdi0=0&luki=8&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="15" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">数据模型</span></a></span>girl对象,<span id="16_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="16_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%CC%EE%B3%E4&k0=%CC%EE%B3%E4&kdi0=0&luki=2&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="16" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">填充</span></a></span>值到xib中各个控件 ,并返回封装好数据之后的,girlCell对象
         - (GirlCell *)cellWithGirl:(Girl *)girl;
 
 7,<span id="17_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="17_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%BF%D8%D6%C6%C6%F7&k0=%BF%D8%D6%C6%C6%F7&kdi0=0&luki=10&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="17" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">控制器</span></a></span>中 cellForRow方法如下
     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     {
         // 把cell封装之后,控制 器知道的东西就非常少了~
         
         // 先从缓存池中,根据ID取出自已的cell类
         GirlCell *girlCell = [tableView dequeueReusableCellWithIdentifier:[GirlCell cellID]];
         if (girlCell == nil) {
         // 如果池中没取到,则类方法,重新生成一个崭新的girlCell,xib界面中指定了重用cellID
         girlCell = [GirlCell girlCell];
         }
         // 设置cell中<span id="18_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="18_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%B6%C0%D2%BB%CE%DE%B6%FE&k0=%B6%C0%D2%BB%CE%DE%B6%FE&kdi0=0&luki=1&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="18" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">独一无二</span></a></span>的内容
         Girl *girl = [_girls objectAtIndex:indexPath.row];
         // 参数为girl对象,<span id="19_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="19_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%CC%EE%B3%E4&k0=%CC%EE%B3%E4&kdi0=0&luki=2&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="19" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">填充</span></a></span>其各个成员值到xib中各个控件 ,并返回填充好的cell
         girlCell = [girlCell cellWithGirl:girl];
         // 返回cell
         return girlCell;
     
     
     }

 
 
 
 */

#import <UIKit/UIKit.h>
@class Girl;
@interface GirlCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UIImageView *headImg;
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UILabel *verdict;

// 返回xib界面中cell的高度
+ (CGFloat)cellHeight;

// 返回xib界面上写的重用cellID
+ (NSString *)cellID;

// 从xib中加载 实例化一个girlCell对象
+ (GirlCell *)girlCell;

// 参数是<span id="20_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="20_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%CA%FD%BE%DD%C4%A3%D0%CD&k0=%CA%FD%BE%DD%C4%A3%D0%CD&kdi0=0&luki=8&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="20" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">数据模型</span></a></span>girl对象,<span id="21_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="21_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%CC%EE%B3%E4&k0=%CC%EE%B3%E4&kdi0=0&luki=2&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="21" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">填充</span></a></span>值到xib中各个控件 ,并返回封装好数据之后的,girlCell对象
- (GirlCell *)cellWithGirl:(Girl *)girl;
@end


GirlCell.m

 

 
//
//  GirlCell.m
//  14_tableViewController_xib自定义cell
//
//  Created by beyond on 14-7-28.
//  Copyright (c) 2014年 com.beyond. All rights reserved.
//

#import "GirlCell.h"
#import "Girl.h"
@implementation GirlCell
// 返回xib界面中cell的高度
+ (CGFloat)cellHeight
{
    // 查看一下xib中cell的高度
    return 80;
}
// 返回xib界面上写的重用cellID
+(NSString *)cellID
{
    // 必须和界面上的一致
    return @"GirlCell";
}

// 从xib中加载 实例化一个girlCell对象
+ (GirlCell *)girlCell
{
    // mainBundel加载xib,扩展名不用写.xib
    NSArray *arrayXibObjects = [[NSBundle mainBundle] loadNibNamed:@"GirlCell" owner:nil options:nil];
    return arrayXibObjects[0];
}
// 返回封装好数据之后的,girlCell对象
- (GirlCell *)cellWithGirl:(Girl *)girl
{
    // 前面,通过<span id="10_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="10_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%C1%AC%CF%DF&k0=%C1%AC%CF%DF&kdi0=0&luki=6&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="10" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">连线</span></a></span>,将xib中的各个控件,连接到GirlCell类,成为它的成员属性了,这样一来就不用通过tag取得xib中每一个控件了
    _name.text = girl.name;
    _headImg.image = [UIImage imageNamed:girl.headImgName];
    _verdict.text = girl.verdict;
    // 返回封装好数据之后的,girlCell对象
    return self;
}
@end



main.storyboard

 

BeyondTableViewController.h

//
//  BeyondTableViewController.h
//  14_tableViewController_xib自定义cell
//
//  Created by beyond on 14-7-28.
//  Copyright (c) 2014年 com.beyond. All rights reserved.
//

#import <UIKit/UIKit.h>

// tableViewController <span id="7_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="7_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%BC%CC%B3%D0&k0=%BC%CC%B3%D0&kdi0=0&luki=5&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="7" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">继承</span></a></span>自UIViewController且已经遵守了数据源和代理,其内部的view就是tableView,且已经自己<span id="8_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="8_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%C1%AC%CF%DF&k0=%C1%AC%CF%DF&kdi0=0&luki=6&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="8" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">连线</span></a></span>了数据源和代理
/*
 创建tableViewController的标准步骤
 1,新建<span id="9_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="9_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%B9%A4%B3%CC&k0=%B9%A4%B3%CC&kdi0=0&luki=3&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="9" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">工程</span></a></span>,然后,删除viewController.h和.m文件
 2,new File 选择OC 类,继承自UITableViewController
 3,到main.storyboard中删除 默认的viewController
 4,拖动一个黄色的tableViewController到main.storyboard,并更改file's owner为自已经建立的BeyondTableViewController
 5,仍然到main.storyboard中删除 自动生成的cell控件
 6,运行既可
 */
@interface BeyondTableViewController : UITableViewController

@end

 

 

BeyondTableViewController.m

 

 
//
//  BeyondTableViewController.m
//  14_tableViewController_xib自定义cell
//
//  Created by beyond on 14-7-28.
//  Copyright (c) 2014年 com.beyond. All rights reserved.
//

#import "BeyondTableViewController.h"
#import "Girl.h"
#import "GirlCell.h"

@interface BeyondTableViewController ()
{
    // 从plist文件中加载的所有girls,返回所有的对象组成的数组
    NSMutableArray *_girls;
}
@end

@implementation BeyondTableViewController

// 隐藏顶部的状态栏
- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 初始化 对象数组
    _girls = [NSMutableArray array];
    
    // plist转成对象数组
    [self plistToObjects];
    
}
// plist转成对象数组
- (void)plistToObjects
{
    // sg_bundle模板代码,1,获得.app主要的包;2,返回主要的包中某个文件的fullPath全路径
    NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *fullPath = [mainBundle pathForResource:@"girls.plist" ofType:nil];
    
    // 从plist文件中根据全路径,返回字典数组
    NSArray *arrayWithDict = [NSArray arrayWithContentsOfFile:fullPath];
    
    // <span id="1_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="1_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%C4%A3%D0%CD&k0=%C4%A3%D0%CD&kdi0=0&luki=9&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="1" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">模型</span></a></span>的类方法返回对象,参数只要一个字典数组即可
    for (NSDictionary *dict in arrayWithDict) {
        // 参数只要字典,这样一来,<span id="2_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="2_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%BF%D8%D6%C6%C6%F7&k0=%BF%D8%D6%C6%C6%F7&kdi0=0&luki=10&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="2" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">控制器</span></a></span>就不用知道太多东西了
        Girl *girl = [Girl girlWithDict:dict];
        // 添加到对象数组
        [_girls addObject:girl];
    }
}

#pragma <span id="3_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="3_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=mark&k0=mark&kdi0=0&luki=4&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="3" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">mark</span></a></span> - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // 返回行数
    return _girls.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 把cell封装之后,控制 器知道的东西就非常少了~
    
    // 先从缓存池中,根据ID取出自已的cell类
    GirlCell *girlCell = [tableView dequeueReusableCellWithIdentifier:[GirlCell cellID]];
    if (girlCell == nil) {
        // 如果池中没取到,则类方法,重新生成一个崭新的girlCell,xib界面中指定了重用cellID
        girlCell = [GirlCell girlCell];
    }
    // 设置cell中<span id="4_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="4_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%B6%C0%D2%BB%CE%DE%B6%FE&k0=%B6%C0%D2%BB%CE%DE%B6%FE&kdi0=0&luki=1&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="4" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">独一无二</span></a></span>的内容
    Girl *girl = [_girls objectAtIndex:indexPath.row];
    // 参数为girl对象,<span id="5_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="5_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=%CC%EE%B3%E4&k0=%CC%EE%B3%E4&kdi0=0&luki=2&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="5" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">填充</span></a></span>其各个成员值到xib中各个控件 ,并返回填充好的cell
    girlCell = [girlCell cellWithGirl:girl];
    // 返回cell
    return girlCell;
    
    
}




#pragma <span id="6_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="6_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=58013af6d4ac7cf3&k=mark&k0=mark&kdi0=0&luki=4&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=f37cacd4f63a0158&ssp2=1&stid=0&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FIOS%2F201407%2F251523%2Eshtml&urlid=0" target="_blank" mpid="6" style="text-decoration: none; color: rgb(90, 128, 238);"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">mark</span></a></span> - 代理方法
// 每一行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 调用类方法,得到xib中的cell的行高
    return [GirlCell cellHeight];
}
// 取消默认点击后,蓝色高亮背景
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}


@end



girls.plist

 

tableViewController层次图


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值