IOS自定義表格UITableViewCell

在UITableView中,自定義表格,最原始是繼承UITableViewCell,而後經過寫代碼方式去搞,可是這個費事了。ios

1.在storyboard中atom

給一個ViewController的tabieview增長自定義的UITableViewCell,能夠直接從 object Library裏面選取UITableViewCell拖動到tableview中,而後添加界面上自定義元素,而後補充cell的類,重用id等信息。spa

補充完成後,須要在工程中添加對應的cell的類文件,並作代碼和xib的關聯。code

@interface DemoCellOne : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *imageInfoView;
@property (weak, nonatomic) IBOutlet UILabel *contentInfoLabel;


@end

而後就能夠在相應的viewcontroller裏面使用了,以下:blog

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DemoCellOne *cell = [tableView dequeueReusableCellWithIdentifier:@"DemoCellOne"];
    cell.contentInfoLabel.text = [self.datasource objectAtIndex:indexPath.row];
    return cell;
}

 

2.在普通的xib文件中繼承

若是ios工程仍是以前那種xib形式的,則能夠給工程添加新文件,添加時候選擇添加新的類,從UITableViewCell繼承,而後在生成源碼文件以前,先在確認界面勾選上生成對應的xib文件。源碼

生成好以後,在xib中給UITableViewCell添加個性化元素,而後在代碼中加載。it

 

如下是cell對應的類的定義,爲了便於修改,作了xib和代碼之間的IBOutlet關聯。io

1 @interface DemoCellTwoTableViewCell : UITableViewCell
2 @property (weak, nonatomic) IBOutlet UILabel *infoLabel;
3 @property (weak, nonatomic) IBOutlet UISegmentedControl *segmentControl;
4 
5 @end

 

如下是在viewcontroller中使用table

@interface ViewController2 ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *contentTableView;
@property(nonatomic,strong) NSMutableArray * datasource;
@end

@implementation ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.contentTableView registerNib:[UINib nibWithNibName:@"DemoCellTwoTableViewCell" bundle:nil] forCellReuseIdentifier:@"DemoCellTwoTableViewCell"];
    self.datasource = [NSMutableArray array];
    [self loadDataSource];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadDataSource
{
    [self.datasource addObject:@"a1"];
    [self.datasource addObject:@"a2"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.datasource count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60.0f;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DemoCellTwoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"DemoCellTwoTableViewCell"];
    cell.infoLabel.text = [self.datasource objectAtIndex:indexPath.row];
    return cell;
}

@end

上面是經過註冊tableview的cell對應的nib文件的方式來重用cell的。還有一種方式以下:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DemoCellTwoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"DemoCellTwoTableViewCell"];
    if (nil == cell)
    {
        NSArray *objs = [[NSBundle mainBundle] loadNibNamed:@"DemoCellTwoTableViewCell" owner:self options:nil];
        cell = [objs objectAtIndex:0];
    }
    cell.infoLabel.text = [self.datasource objectAtIndex:indexPath.row];
    return cell;
}

這種方式不須要在viewcontroller的viewdidload方法裏面註冊重用的nib文件。只是在cell重用處加載nib。

 

注意:第二種方式,在自定義cell的xib文件中,file owner不須要修改,保持默認值就好了。

相關文章
相關標籤/搜索