IOS自定义表格UITableViewCell

在UITableView中,自定义表格,最原始是继承UITableViewCell,然后通过写代码方式去搞,但是这个费事了。

1.在storyboard中

给一个ViewController的tabieview增加自定义的UITableViewCell,可以直接从 object Library里面选取UITableViewCell拖动到tableview中,然后添加界面上自定义元素,然后补充cell的类,重用id等信息。

补充完成后,需要在工程中添加对应的cell的类文件,并做代码和xib的关联。

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


@end

然后就可以在相应的viewcontroller里面使用了,如下:

- (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添加个性化元素,然后在代码中加载。

以下是cell对应的类的定义,为了便于修改,做了xib和代码之间的IBOutlet关联。

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

以下是在viewcontroller中使用

@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不需要修改,保持默认值就行了。

原文地址:https://www.cnblogs.com/leipei2352/p/4358377.html