标签:
UITableViewCell的创建方式
方式1 直接通过alloc:
//返回当前行显示的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"cellForRowAtIndexPath-%ld-%ld",indexPath.section,indexPath.row);
UITableViewCell *cell = [[UITableViewCell alloc] init];
if (indexPath.section == 0) {
//第一组
if (indexPath.row == 0) {
//第一组 第一行
cell.textLabel.text = @"张三";
}else if(indexPath.row == 1){
//第一组 第2行
cell.textLabel.text = @"李四";
}else{
//第一组 第3行
cell.textLabel.text = @"王五";
}
}else{
//第二组
if(indexPath.row == 0){
cell.textLabel.text = @"刘德华";
}else{
cell.textLabel.text = @"张学友";
}
}
return cell;
}
方式2 通过tableView的静态单元格方式,此方式只是静态的,固定的行:


这样就可以实现静态表格内容
方式3 通过xib方式:

//返回每行显示的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//1 创建可重用的cell
static NSString *reuseId = @"gb";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
if (cell == nil) {
// cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseId];
//从xib中加载cell
cell = [[[NSBundle mainBundle] loadNibNamed:@"CZGroupBuyingCell" owner:nil options:nil] lastObject];
//从xib中加载view的另一种方式
// UINib *nib = [UINib nibWithNibName:@"CZGroupBuyingCell" bundle:nil];
// cell = [[nib instantiateWithOwner:nil options:nil] lastObject];
}
//2 设置cell内部的子控件
//2.1 获取当前要展示的数据
CZGroupBuying *gb = self.groupBuyings[indexPath.row];
//2.2 设置值
// cell.textLabel.text = gb.title;
// cell.detailTextLabel.text = [NSString stringWithFormat:@"¥%@",gb.price];
// cell.imageView.image = [UIImage imageNamed:gb.icon];
UILabel *titleView = (UILabel *)[cell viewWithTag:10];
titleView.text = gb.title;
//3 返回
return cell;
}
自定义cell


定义cell中放入的控件,自动归为Cell的content view视图的子视图
@class CZGroupBuying; @interface CZGroupBuyingCell : UITableViewCell @property (nonatomic, strong) CZGroupBuying *groupBuying; + (instancetype)groupBuyingCellWithTableView:(UITableView *)tableView; @end
@interface CZGroupBuyingCell ()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *titleView;
@property (weak, nonatomic) IBOutlet UILabel *priceView;
@property (weak, nonatomic) IBOutlet UILabel *buyCountView;
@end
@implementation CZGroupBuyingCell
//创建自定义可重用的cell对象
+ (instancetype)groupBuyingCellWithTableView:(UITableView *)tableView
{
static NSString *reuseId = @"gb";
CZGroupBuyingCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"CZGroupBuyingCell" owner:nil options:nil] lastObject];
}
return cell;
}
//重写属性的setter方法,给子控件赋值
- (void)setGroupBuying:(CZGroupBuying *)groupBuying
{
_groupBuying = groupBuying;
self.titleView.text = groupBuying.title;
self.priceView.text = [NSString stringWithFormat:@"¥ %@",groupBuying.price];
self.buyCountView.text = [NSString stringWithFormat:@"%@人已购买",groupBuying.buyCount];
self.iconView.image = [UIImage imageNamed:groupBuying.icon];
}
#warning 先从换成池中取,如果缓存池中没有可循环利用的cell,先去storyboard或xib中找合适的cell ,cell是从xib中创建出来的,而不是手写代码方式alloc出来的
static NSString *reuseId = @"gb";
CZGroupBuyingCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
从xib或storyboard中创建的cell,创建完毕后系统会调用awakeFromNib方法
/**
* 如果cell是通过storyboard或者xib创建的,就会调用这个方法来初始化cell
* 这个方法的作用类似于init方法
*/
- (void)awakeFromNib {
NSLog(@"%s",__func__);
// Initialization code
// 自定义表格分割线
UIView *separatorView = [[UIView alloc] init];
#warning 添加至cell的内容视图中
[self.contentView addSubview:separatorView];
self.separatorView = separatorView;
separatorView.alpha = 0.5;
separatorView.backgroundColor = [UIColor redColor];
}
不会调用initwithStyle方法 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
在获取cell使用创建和赋值操作
//返回每行显示的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//1 创建可重用的自定义的cell
CZGroupBuyingCell *cell = [CZGroupBuyingCell groupBuyingCellWithTableView:tableView];
//2 设置cell内部的子控件
CZGroupBuying *gb = self.groupBuyings[indexPath.row];
cell.groupBuying = gb;
//3 返回
return cell;
}
方式4 通过storyboard 通过此方式一般是使用tableview的动态原型功能:



获取storyboard中cell的方式
+ (instancetype)tableViewCell:(UITableView *)tableView{
#warning 先从换成池中取,如果缓存池中没有可循环利用的cell,先去storyboard中找合适的cell ,cell是从storyboard中创建出来的,而不是手写代码方式alloc出来的
// 此标识要和storyboard 本控制器中的动态cell设置的identify标识一致
static NSString *ID = @"contactCell";
return [tableView dequeueReusableCellWithIdentifier:ID];
}
通过tableview dequeueReusableCellWithIdentifier:ID 系统就会去storyboard中找合适的cell
/**
* 如果cell是通过storyboard或者xib创建的,就会调用这个方法来初始化cell
* 这个方法的作用类似于init方法
*/
- (void)awakeFromNib {
NSLog(@"%s",__func__);
// Initialization code
// 自定义表格分割线
UIView *separatorView = [[UIView alloc] init];
#warning 添加至cell的内容视图中
[self.contentView addSubview:separatorView];
self.separatorView = separatorView;
separatorView.alpha = 0.5;
separatorView.backgroundColor = [UIColor redColor];
}
标签:
原文地址:http://www.cnblogs.com/HJiang/p/4197123.html