UI之UITableView和UITableViewCell

一、UITableView

1.UITableView是我们经常需要用到的UI控件,它继承于UIScrollView类,因此可以进行垂直滚动。
2.要想实现UITableView必须遵守UITableViewDelegate和UITableViewDataSource两个协议。
创建UITableView:

// 初始化tableView
personalTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT) style:UITableViewStyleGrouped];
// 将tableView的代理给控制器本身
personalTableView.delegate = self;
personalTableView.dataSource = self;
// 设置分割线样式
personalTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
[self.view addSubview:personalTableView];

UITableViewDataSource协议必须要实现的两个方法:

1.每个部分包含的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
2.每一行要显示的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
二、UITableViewCell

UITableView的每一个数据对应的单元格称为cell,是UITableViewCell类的对象,继承于UIView类。
采用可重用标志创建每行要显示的cell,这样当一个cell滑出屏幕另一个cell出现时并不是销毁再重新创建,而是从重用池中获取:

- (UITableViewCell *) tableView : (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier : CellIdentifier];
    if (!cell) {     
    cell = [[UITableViewCell alloc] initWithStyle : UITableViewCellStyleSubtitle reuseIdentifier: CellIdentifier];
    }
    cell.imageView.image = [UIImage imageNamed:@"the ordinary world"];
    cell.textLabel.text = [array objectAtIndex : [indexPath row]]; 
    cell.detailTextLabel.text = @"作者:路遥";
    return cell;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值