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

自定义UITableViewCell

2016-02-24 00:00 405 查看
1、创建xib

2、创建一个继承UITableViewCell的控制器

3、xib和控制器绑定起来。

4、用 NSBundle加载

5、设置自适应高度。

#pragma mark 多少条
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return 6;
}

#pragma mark 加载cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *cellID=@"blogcell";
UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:cellID];
if (cell==nil) {

cell=[[[NSBundle mainBundle]loadNibNamed:@"BlogCell" owner:self options:nil] lastObject];

}

UILabel * label=[cell viewWithTag:2];
label.text=@"大灰狼";

return cell;

}

#pragma mark 自适应高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [self tableView:_tableView cellForRowAtIndexPath:indexPath];

return cell.frame.size.height;
}


最后前面别忘记引入UITableViewDataSourceDelegate和UITableViewDelegate 并设置代理
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: