iOS(学习) UITableView

UITableView有两种风格:UITableViewStylePlain和UITableViewStyleGrouped。这两者操作起来其实并没有本质区别,只是后者按分组样式显示前者按照普通样式显示而已。

UITableViewStylePlain:

这里写图片描述

UITableViewStyleGrouped

这里写图片描述

UITableView中每行数据都是一个UITableViewCell,在这个控件中为了显示更多的信息,iOS已经在其内部设置好了多个子控件以供开发者使用。如果我们查看UITableViewCell的声明文件可以发现在内部有一个UIView控件(contentView,作为其他元素的父控件)、两个UILable控件(textLabel、detailTextLabel)、一个UIImage控件(imageView),分别用于容器、显示内容、详情和图片。使用效果类似于微信

这里写图片描述

    表视图
 UITableView
    1.UITableView 继承于 UIScrollView 可以滚动
    2.为什么需要 UITableView ? 当有很多内容要展示的时候 UIScrollView就不能满足开发需求 ,
    3.为什么UITableView 可以展示很多内容?  使用类似手扶电梯一样‘重用机制’
    4.UITableView 的每一条数据对应的单元格叫做 call 是UITableViewCell的一个对象。继承UIView
      UITableView 可以显示分区, 每一个分区,每一个分区称 ‘section’, 每一行row, 编号从0开始。
        系统提供了一个专门的类来整合。 section 和 row ,NSIndexPath
       section 和 row 代表一个 TableViewCall 在 UITableView 上的位置
#import "ViewController.h"
#import "HeedTableViewCell.h"
#import "DetailTableViewCell.h"
#define CellIdentifierHeader @"CellIdentifierHeader"
#define CellIdentifierDetail @"CellIdentifierDetail"
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
{
    NSMutableArray *_dataSource;
}
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    //

    // 判断当前系统是否 是7.0以上的
    if ([[[UIDevice currentDevice] systemVersion]floatValue] > 7.0?YES:NO) {

        // 这个方法是7.0 之后才有的
        // 这个方法有什么作用 :解决了导航栏的一些缺陷
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }

    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height) style:UITableViewStylePlain];

    // 这两个属性 要遵循代理
    tableView.dataSource = self;
    tableView.delegate = self;

    /**
     *  // 设置分区的高度 想要分区高度统一 就可以这样设置,
     *  // 多个分区 分区高度不一样  用这个方法设置:- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
     */
   // tableView.rowHeight = 103;
    [self.view addSubview:tableView];

    /**
     // 自定义的  注册表视图
        [tableView registerNib:<#(nullable UINib *)#> forCellReuseIdentifier:<#(nonnull NSString *)#>]

     //系统的 注册表视图
        [tableView registerClass:<#(nullable Class)#> forCellReuseIdentifier:<#(nonnull NSString *)#>]

     */


    // 注册表视图
    [tableView registerNib:[UINib nibWithNibName:@"HeedTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:CellIdentifierHeader];
    [tableView registerNib:[UINib nibWithNibName:@"DetailTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:CellIdentifierDetail];


    NSArray *firstArray = @[@"好友动态", @"附近的人", @"兴趣部落"];

    NSArray *secondArray = @[@"游戏",@"购物", @"音乐", @"直播", @"附近的群"];
    NSArray *imageArray = @[[UIImage imageNamed:@"yx"], [UIImage imageNamed:@"gw"], [UIImage imageNamed:@"yy"], [UIImage imageNamed:@"zb"], [UIImage imageNamed:@"fjq"]];

    // 大数组 圈套小数组
    _dataSource = [NSMutableArray arrayWithObjects:firstArray, secondArray, imageArray, nil];

}
#pragma mark ----  UITableViewDataSource, UITableViewDelegate  ----
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // 判断分区 返回多少行

    NSInteger num = 0;
    switch (section) {
        case 0: {
            num = 1;
        }
            break;
        case 1:{
            NSArray *secondArray = _dataSource[1];
            num = secondArray.count;
        }

        default:
            break;
    }

    return num;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {



    UITableViewCell *cell;

    if (indexPath.section == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierHeader forIndexPath:indexPath];
        HeedTableViewCell *headerCell = (HeedTableViewCell *)cell;

        NSArray *firstArray = _dataSource[0];


            headerCell.friendLabel.text = firstArray[0];
            headerCell.nearbyLabel.text = firstArray[1];
            headerCell.clubLabel.text = firstArray[2];

        [headerCell.friendBtn setImage:[UIImage imageNamed:@"qq"] forState:UIControlStateNormal];
        [headerCell.nearbyBtn setImage:[UIImage imageNamed:@"fj"] forState:UIControlStateNormal];
        [headerCell.clubBut setImage:[UIImage imageNamed:@"xq"] forState:UIControlStateNormal];

    } else {

        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierDetail forIndexPath:indexPath];
        DetailTableViewCell *detailCell = (DetailTableViewCell *)cell;

        NSArray *imageArray1 = _dataSource[2];
        detailCell.iconImageView.image = imageArray1[indexPath.row];

        NSArray *secondArray = _dataSource[1];
        detailCell.titleLabel.text = secondArray[indexPath.row];

        // 这是系统的 按钮
        detailCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }

//    HeedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierHeader forIndexPath:indexPath];

//    cell.textLabel.text = @"zyx";
//    cell.friendLabel.text = @"好友动态";
//    cell.nearbyLabel.text = @"附近的人";
//    cell.clubLabel.text = @"兴趣部落";

    return cell;

}
// 返回多少个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
// 修改 分区的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height;
    switch (indexPath.section) {
        case 0:
            height = 103;
            break;
        case 1:
            height = 58;
        default:
            break;
    }
    return height;
}
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    NSString *string;

    switch (section) {
        case 0:
            string = @"A";
            break;
        case 1:
            string = @"B";
            break;
        default:
            break;
    }
    return string;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

运行效果:

效果图.png

源码下载:GitHub

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值