您的位置:首页 > 产品设计 > UI/UE

从nib文件加载自定义的UITableviewCell

2014-04-04 15:56 501 查看
[csharp] view
plaincopy

也许我们很多人都清楚怎么定制属于我们自己的UItableviewcell, 习惯了代码的我,竟然今天才彻底的理解了从nib文件加载自己定制的cell。  

[javascript] view
plaincopy

registerNib:forCellReuseIdentifier:  

Registers a nib object containing a cell with the table view under a specified identifier.  

  

- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier  

Parameters  

nib  

A nib object that specifies the nib file to use to create the cell. This parameter cannot be nil.  

identifier  

The reuse identifier for the cell. This parameter must not be nil and must not be an empty string.  

Discussion  

Prior to dequeueing any cells, call this method or the registerClass:forCellReuseIden
4000
tifier: method to tell the table view how to create new cells. If a cell of the specified type is not currently in a reuse queue, the table view uses the provided information to create a new cell object automatically.  

  

If you previously registered a class or nib file with the same reuse identifier, the nib you specify in the nib parameter replaces the old entry. You may specify nil for nib if you want to unregister the nib from the specified reuse identifier.  

  

Availability  

Available in iOS 5.0 and later.  

See Also  

tableView:cellForRowAtIndexPath: (UITableViewDataSource)  

Declared In  

UITableView.h  

从官网的文档可以看出,通过 registerNib:forCellReuseIdentifier 这个方法可以实现。在使用复用机制之前要先确保 registernib这个方法,这个方法就是告诉了table是通过nib文件来创建cell的,这点很重要。

下面贴上主要的代码,这个是.h文件

[csharp] view
plaincopy

//  

//  ViewController.h  

//  TestCell  

//  

//  Created by Juncy_Fan on 13-3-11.  

//  Copyright (c) 2013年 Juncy_Fan. All rights reserved.  

//  

 

#import <UIKit/UIKit.h>  

  

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{  

    UITableView *table;  

    UINib *nib;  

}  

  

@end  

下面的是.m文件

[csharp] view
plaincopy

//  

//  ViewController.m  

//  TestCell  

//  

//  Created by Juncy_Fan on 13-3-11.  

//  Copyright (c) 2013年 Juncy_Fan. All rights reserved.  

//  

 

#import "ViewController.h"  

#import "MyCell.h"  

  

@interface ViewController ()  

  

@end  

  

@implementation ViewController  

static BOOL nibsRegistered;  

- (void)viewDidLoad  

{  

    [super viewDidLoad];  

    nibsRegistered = NO;  

    NSLog(@"初始化:%d",nibsRegistered);  

    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];  

    [self.view addSubview:table];  

    table.delegate = self;  

    table.dataSource = self;  

    [table release];  

}  

  

- (void)didReceiveMemoryWarning  

{  

    [super didReceiveMemoryWarning];  

    // Dispose of any resources that can be recreated.  

}  

  

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

    return 300;  

}  

  

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

    static NSString *identy = @"CustomCell";  

    if (!nib) {  

        nib = [UINib nibWithNibName:@"MyCell" bundle:nil];  

        [tableView registerNib:nib forCellReuseIdentifier:identy];  

        NSLog(@"我是从nib过来的,%d",indexPath.row);  

    }  

    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:identy];  

    NSUInteger row = [indexPath row];  

    if (row%2 == 0) {  

        cell.myTitle.text = @"我是偶数行的";  

        cell.mySubTitle.text = @"我是子标题";  

        cell.imageicon.image = [UIImage imageNamed:@"201202.png"];  

    }else{  

        cell.myTitle.text = @"我是奇数行的";  

        cell.mySubTitle.text = @"我是奇数行的子标题";  

        cell.imageicon.image = [UIImage imageNamed:@"201203.png"];  

    }  

    return cell;  

}  

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

    return 100;  

}  

  

@end  

ok啦,定制cell的时候,nib文件里面identifer一定要和代码里面的保持一致哦。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  自定义cell