使用 xib 创建 tableViewCell

本文介绍如何在iOS应用中使用UITableView创建自定义单元格,并展示了如何设置单元格数量、高度及加载不同样式的单元格。
#import "ViewController.h"
#import "PlayerCell.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableview;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self  createTableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}

-(void)createTableView{
    _tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableview.backgroundColor = [UIColor groupTableViewBackgroundColor];
    _tableview.dataSource = self;
    _tableview.delegate = self;
    [self.view  addSubview:_tableview];

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"cell";
    PlayerCell *cell = [tableView  dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil){
//#ifdef _1
      //  [tableView registerNib:[UINib nibWithNibName:@"PlayerCell" bundle:nil] forCellReuseIdentifier:@"cell"];
//        cell = [tableView  dequeueReusableCellWithIdentifier:cellID];
//#elif  _0
       cell = [[[NSBundle mainBundle] loadNibNamed:@"PlayerCell" owner:self options:nil]lastObject];
//#endif
            }
  cell.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];

    return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{\
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 6;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值