在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不须要修改,保持默认值就好了。