前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS-UITableView 详解(一)

iOS-UITableView 详解(一)

作者头像
xx_Cc
发布2018-05-10 11:32:42
1.2K0
发布2018-05-10 11:32:42
举报

iOS-UITableView 详解 (一)

✨建议收藏,用到时候一查就明白了 UITableView可以说是iOS开发中最重要的控件之一,它的使用非常广泛,今天我们来学习UITableView的使用。

基本介绍:

UITableView有两种风格:UITableViewStylePlainUITableViewStyleGrouped。这两者操作起来其实并没有本质区别,只是后者按分组样式显示前者按照普通样式显示而已 大家先看一下两者的区别:

UITableViewStylePlain

不分组样式UITableViewStylePlain

UITableViewStyleGrouped

分组样式UITableViewStyleGrouped

UITableViewCell

UITableView中每行都是一个UITableViewCell,UITableViewCell的样式我们可以通过UITableViewCellStyle进行设置,UITableViewCellStyle是一个枚举值,我们来看看UITableViewCell都有哪些样式

代码语言:javascript
复制
typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
UITableViewCellStyleDefault, // 左侧显示textLabel(不显示detailTextLabel),imageView可选(显示在最左边) 
UITableViewCellStyleValue1, // 左侧显示textLabel、右侧显示detailTextLabel(默认蓝色),imageView可选(显示在最左边)
UITableViewCellStyleValue2, // 左侧依次显示textLabel(默认蓝色)和detailTextLabel,imageView可选(显示在最左边) 
UITableViewCellStyleSubtitle // 左上方显示textLabel,左下方显示detailTextLabel(默认灰色),imageView可选(显示在最左边)
 };

简单使用

接下来我们先来完成一个简单的UITableView的使用 首先看一下数据结构关系

cars_total.plist

可以看到数组里面包含若干个字典,字典里两个键值,一个是汽车数组,一个是标题,数组中又是若干个字典,分别是icon键值和 name键值

接下来我们来创建汽车模型 CLCar.h

代码语言:javascript
复制
#import <Foundation/Foundation.h>

@interface CLCar : NSObject
@property(nonatomic,copy)NSString *icon;
@property(nonatomic,copy)NSString *name;

-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)carWithDict:(NSDictionary *)dict;

+(NSArray *)carsWithArray:(NSArray *)array;

@end

CLCar.m

代码语言:javascript
复制
#import "CLCar.h"

@implementation CLCar

-(instancetype)initWithDict:(NSDictionary *)dict
{
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}
+(instancetype)carWithDict:(NSDictionary *)dict
{
    return [[self alloc]initWithDict:dict];
}

+(NSArray *)carsWithArray:(NSArray *)array
{
    NSMutableArray *arrayM = [NSMutableArray array];
    
    for (NSDictionary *dic in array) {
        [arrayM addObject:[self carWithDict:dic]];
    }
    return arrayM;
}

@end

CLCarGroup.h

代码语言:javascript
复制
#import <Foundation/Foundation.h>

@interface CLCarGroup : NSObject

@property(nonatomic,strong)NSArray *cars;
@property(nonatomic , copy)NSString *title;

-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)carGroupWithDict:(NSDictionary *)dict;

+(NSArray *)carGroups;

@end

CLCarGroup.m

代码语言:javascript
复制
#import "CLCarGroup.h"
#import "CLCar.h"
@implementation CLCarGroup

-(instancetype)initWithDict:(NSDictionary *)dict
{
    self = [super init];
    if (self) {
        [self setValue:dict[@"title"] forKey:@"title"];
        self.cars = [CLCar carsWithArray:dict[@"cars"]];
    }
    return self;
}
+(instancetype)carGroupWithDict:(NSDictionary *)dict
{
    return [[self alloc]initWithDict:dict];
}

+(NSArray *)carGroups
{
    NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"cars_total.plist" ofType:nil]];
    NSMutableArray *arrayM = [NSMutableArray array];
    for (NSDictionary *dict in array) {
        [arrayM addObject:[self carGroupWithDict:dict]];
    }
    return arrayM;
}
@end

模型创建好了,我们就可以把他们显示在UITableView上啦 ViewController.h

代码语言:javascript
复制
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end

ViewController.m

代码语言:javascript
复制
#import "ViewController.h"
#import "CLCarGroup.h"
#import "CLCar.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>// 遵守协议

@property(nonatomic,strong)NSArray *carGroups;
@property(nonatomic ,strong)UITableView *tableView;
@end

@implementation ViewController

-(NSArray *)carGroups
{
    if (_carGroups == nil) {
        _carGroups = [CLCarGroup carGroups];
    }
    return _carGroups;
}

-(UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.dataSource = self;// 设置代理
        _tableView.delegate = self; // 设置代理
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@",self.carGroups);
    [self tableView];
}

#pragma mark - UITableViewDataSource 数据源方法 
// 以下两个方法是UITableViewDataSource 中required 必须实现的方法
// 返回每组行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    CLCarGroup *group = self.carGroups[section];
    return group.cars.count;
}

// TableViewCell 内容的设置
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil ) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    CLCarGroup *group = self.carGroups[indexPath.section];
    CLCar *car = group.cars[indexPath.row];
    
    cell.imageView.image = [UIImage imageNamed:car.icon];
    cell.textLabel.text = car.name;
    
    return cell;
    
}

// 以下方法是UITableViewDataSource 中optional 选择实现的方法

// 返回分组数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.carGroups.count;
}

//返回分组的头标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    CLCarGroup *group = self.carGroups[section];
    return group.title;
}
//返回分组的脚标题
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    CLCarGroup *group = self.carGroups[section];
    return group.title;
}
// 右侧索引 ,返回数组
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return [self.carGroups valueForKeyPath:@"title"];
}

#pragma mark -UITableViewDelegate的代理方法
//返回头分组标题高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}
//返回脚分组标题高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 50;
}
// 返回行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
@end

这个时候我们发现,当我们设置UITableViewstyleUITableViewStylePlain时,我们依然实现返回分组数,和返回分组头标题两个方法,这时当我们滑动tableVIew时,头标题还会自动停留的屏幕最上方,效果如下

头标题演示

本文借鉴了很多前辈的文章,如果有不对的地方请指正,欢迎大家一起交流学习

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.07.04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • iOS-UITableView 详解 (一)
    • 基本介绍:
      • UITableViewCell
        • 简单使用
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档