UITableView

一:关于UITableView

  1. 在iOS的应用中最为常见,例如:短信,电话,微信等
  2. 继承自UIScrollView,拥有它的所有特性
  3. 跟大部分控件一样,是UIKit在UIKit库中 001
  4. 只分行,不分列,易操作(扩展:可以分行分列的是UICollectionView,功能更强大,加下来会讲,到时候会一起来说最常用的为什么是tableView,用兴趣的可以自己提前看一下)002
  5. 具有强大功能并且使用方便,封装完善,最早的一批空间,而collection是6.0

二:基本介绍

  1. UITableView有两种风格:UITableViewStylePlain、UITableViewStyleGrouped
  2. UITableView中每行数据都是一个UITableViewCell
  3. 头视图、尾视图,组头视图、组尾视图 003
  4. 代理delegate,数据源代理dataSource,004
    909891-474009a4ee80de0d.png
    UITableView的代理.png
909891-e8084cdcb2f3a7f5.png
UITableViewDataSource.png
909891-a7bdb84b9d7f8224.png
UITableViewDelegate.png

5.tableView为了节省内存,使用复用机制,复用的机制

三:UITableViewCell基本介绍

  1. 四种样式
typedef NS_ENUM(NSInteger, UITableViewCellStyle) { 
    UITableViewCellStyleDefault,    // 左侧显示textLabel(不显示detailTextLabel),imageView可选(显示在最左边) 
    UITableViewCellStyleValue1,        // 左侧显示textLabel、右侧显示detailTextLabel(默认蓝色),imageView可选(显示在最左边) 
    UITableViewCellStyleValue2,        // 左侧依次显示textLabel(默认蓝色)和detailTextLabel,imageView可选(显示在最左边) 
    UITableViewCellStyleSubtitle    // 左上方显示textLabel,左下方显示detailTextLabel(默认灰色),imageView可选(显示在最左边)
}; 
  1. 一些复杂的页面可以进行自定义样式

注:代码实现

//
//  PBViewController.m
//  CBSS_iOS
//
//  Created by mass on 2017/10/18.
//  Copyright © 2017年 Bin_Peng. All rights reserved.
//

#import "PBViewController.h"

@interface PBViewController ()<UITableViewDataSource,UITableViewDelegate>//签订代理协议
@end

@implementation PBViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //创建tableView
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain];
    //设置代理对象
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];
}

//实现代理
#pragma mark -<UITableViewDataSource,UITableViewDelegate>-
#pragma mark -数据源代理
//共有多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 6;
}
//每组有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}
//每行的具体展示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *identify = @"cellIdengtify";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"我是第%ld组%ld行",indexPath.section,indexPath.row];
    return cell;
}


#pragma mark -操作代理
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50.0f;//不设置,默认44.0f;
}
//组头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30.0f;
}
//组尾高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 30.0f;
}
//组头视图
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NSString *identify = @"headerIdengtify";
    UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identify];
    if (!headerView) {
        headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:identify];
    }
    headerView.textLabel.text = [NSString stringWithFormat:@"我是第%ld组头",section];
    return headerView;
}
//组尾视图
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    NSString *identify = @"footerIdengtify";
    UITableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identify];
    if (!footerView) {
        footerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:identify];
    }
    footerView.textLabel.text = [NSString stringWithFormat:@"我是第%ld组尾",section];
    return footerView;
}
@end
909891-63374edfa8c6d3ac.png
程序截图.png
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值