1.使用xib自定义 cell

1.1 加载nib方法,使用默认cell

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    static NSString*ID =@"MyCell";
    UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:ID];
    //如果没有在缓冲池找到cell,实例化
    
    if(cell ==nil)
    {
        //        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        // MyCell XIB文件中定义的Objects数组
        NSArray*array = [[NSBundlemainBundle]loadNibNamed:@"MyCell"owner:niloptions:nil];
        cell = array.lastObject;
        //使用tag取出按钮
        UIButton*button = (UIButton*)[cellviewWithTag:103];
        //增加监听方法
        [buttonaddTarget:selfaction:@selector(buy:forEvent:)forControlEvents:UIControlEventTouchUpInside];
    }
    
    // 1)名称
    NSString*str = [NSStringstringWithFormat:@"商品-%02d", indexPath.row];
    UILabel*label = (UILabel*)[cellviewWithTag:kStartTag];
    [labelsetText:str];
    //    [cell.textLabel setText:str];
    // 2)图片
    // 3)日期
    return cell;
}

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    return80;
}

#pragma mark -按钮操作
- (void)buy:(id)sender forEvent:(UIEvent*)event
{
    NSLog(@"come here");
    // 1)取出cell
    //触摸-Touch,支持多点触摸
    //取出所有手指的触摸
    NSSet*set = [eventallTouches];
    //取出任意手指的触摸
    UITouch*touch = [setanyObject];
    //根据手指的触摸,找到触摸位置
    CGPointlocation = [touchlocationInView:self.tableView];
    //根据手指触摸位置找到对应的单元格
    NSIndexPath*indexPath = [self.tableViewindexPathForRowAtPoint:location];
    NSLog(@"%@", indexPath);
}
View Code

此方法关联点击事件,在ViewController不能采取联线的方法关联事件,必须自定义事件

 

1.2 使用注册的方法自定义单元格,不加载nib文件

static NSString*MyCellID =@"MyCell”;
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"%p %p",self.view,self.tableView);
    //使用XIB的方式自定义UITableView单元格,可以在viewDidLoad中注册该xib文件
    //注册的目的是:简化表格的优化
    //如果bundle=nil,默认从mainBundle中查找对应的nib文件
    // 1)实例化nib
    UINib *nib = [UINib nibWithNibName:@"MyCell" bundle:nil];
    // 2)为表格注册可重用nib
    [self.tableView registerNib:nib forCellReuseIdentifier:MyCellID];
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:MyCellID];
    // 1)名称
    NSString*str = [NSStringstringWithFormat:@"商品-%02d", indexPath.row];
    //一般情况下,需要少用viewWithTag方法,因为这个方法的效率不高
    UILabel*label = (UILabel*)[cellviewWithTag:kStartTag];
    [labelsetText:str];
    // 2)使用tag取出button
    UIButton*button = (UIButton*)[cellviewWithTag:103];
    //在为按钮添加监听方法前,需要判断按钮是否已经被监听
    if([buttonallTargets].count==0) {
        [buttonaddTarget:selfaction:@selector(buy:forEvent:)forControlEvents:UIControlEventTouchUpInside];
        NSLog(@"监听数量:%d", button.allTargets.count);
    }
    
    //    [cell.textLabel setText:str];
    // 2)图片
    // 3)日期
    return cell;
}
View Code

 

1.3 继承UITableViewCell,关联xib文件,在初始化中进行注册

1.class设置为BookCell

2.style设置为Custom

3.Identifier设置为bookCell

 

BookCell.xib文件中


BookCell.h
@interface BookCell :UITableViewCell
//书名
@property(weak,nonatomic)IBOutletUILabel*bookNameLabel;
//价格
@property(weak,nonatomic)IBOutletUILabel*priceLabel;
- (IBAction)dianji:(id)sender;
//购买
- (IBAction)purchase:(id)sender;
//收藏
- (IBAction)privaties:(id)sender;
@end

BookCell.m
#import"BookCell.h"
@implementationBookCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
{
    self= [superinitWithStyle:stylereuseIdentifier:reuseIdentifier];
    if(self) {
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [supersetSelected:selectedanimated:animated];
}

#pragma mark - Actions

- (IBAction)purchase:(id)sender
{
    NSLog(@"购买%@",_bookNameLabel.text);
}

- (IBAction)privaties:(id)sender
{
    NSLog(@"收藏%@",_priceLabel.text);
}

- (IBAction)dianji:(id)sender
{
    NSLog(@"点击");
}
@end

2.3 注册xib方法,对于类名进行注册
- (void)viewDidLoad
{
    //注意:以下几句注册XIB的代码,一定要在viewDidLoad中!
    //注册XIB文件
    UINib*nib = [UINib nibWithNibName:@"BookCell"bundle:[NSBundle mainBundle]];
    //获得根视图,并且转换成TableView
    UITableView*tableView = (UITableView*)self.view;
    //为tableView注册xib的标识符,1.注册cell要用到的xib
    [tableView registerNib:nib forCellReuseIdentifier:@"bookCell”];
}
     
#pragma mark每一行显示的内容
 - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
 {
    // 1.创建一个静态表格标示字符串,定义静态变量时,变量首字母要大写
    static NSString*CellIdentifier =@"bookCell";
    
    // 2.从缓存池查找是否有可用的表格行对象
    BookCell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    // 4.设置单元格内容
    Book*book =_dataList[indexPath.row];
    cell.bookNameLabel.text= book.bookName;
    cell.priceLabel.text= [NSString stringWithFormat:@"$%.2f", book.price];
    return cell;
}
 
#pragma mark -代理方法
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    return80.0;
    
}
View Code

 

 

1.4 继承UITableViewCell,关联xib文件,但是不需要进行调用registerNib进行注册关联,需要加载nib文件方法

@interface Input_GlobalKey_Cell : UITableViewCell

#import "Input_GlobalKey_Cell.h"

@implementation Input_GlobalKey_Cell

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

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


//在Class类中关联Input_GlobalKey_Cell类中,
在Identifier中注册标识符名称,Input_GlobalKey_Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MailCellIdentifier = @"Input_LeftImgage_Cell";
    static NSString *KeyCellIdentifier = @"Input_GlobalKey_Cell";

    
    if (indexPath.section == 0) {
        Input_LeftImgage_Cell *cell = [tableView dequeueReusableCellWithIdentifier:MailCellIdentifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"Input_LeftImgage_Cell" owner:self options:nil] firstObject];
        }
        [cell configWithImgName:@"login_email" andPlaceholder:@"电子邮箱" andValue:self.myRegister.email];
        cell.textValueChangedBlock = ^(NSString *valueStr){
            self.myRegister.email = valueStr;
        };
        return cell;
    }
}
View Code

 

posted on 2015-07-05 11:02  pTrack  阅读(160)  评论(0)    收藏  举报