IOS UITableView详解一数据展示 & 页面简单实现

一 UITableView简介

UITableView用于表格数据展示,是使用频率最高的控件。UITableView继承自UIScrollView,具有UIScrollView的所有特性。



二 UITableView的两种样式
2.1 UITableViewStylePlain                                   2.2 UITableViewStyleGrouped

230042_XGFz_1032974.png                     230242_81nC_1032974.png 




UITableView数据展示

UITableView需要数据源(dataSource)来显示数据,实现UITableViewDataSource协议的对象,都可以是UITableView的数据源。通过协议中的方法,UITableView显示相应的行数以及每行显示的数据。


3.1 调用数据源的以下方法获取一共有多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;


3.2 调用数据源的以下方法获取每一组有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;


3.3 调用数据源的以下方法获取每一行显示什么内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;




UITableViewCell

UITableView每行都是一个UITableViewCell,通过dataSource的tableView:cellForRowAtIndexPath:方法来初始化每行


UITableViewCell内部有个默认的子视图:contentView,contentView是UITableViewCell所显示内容的父视图,可以通过设置UITableViewCell的accessoryType来显示一些辅助指示视图,辅助指示视图用于表示动作的图标。

4.1 辅助指示视图
UITableViewCell的accessoryType值图标样式
UITableViewCellAccessoryNone(默认)不显示辅助指示视图
UITableViewCellAccessoryDisclosureIndicator185130_pzIH_1032974.png
UITableViewCellAccessoryDetailDisclosureButton224335_XCrF_1032974.png
UITableViewCellAccessoryCheckmark185130_ywMQ_1032974.png
UITableViewCellAccessoryDetailButton224257_cDxI_1032974.png

注:还可以通过UITableViewCell的accessoryView属性来自定义辅助指示视图



4.2 contentView 的子视图
//4.2.1 textLabel
@property (nonatomic, readonly, retain) UILabel *textLabel;

//4.2.2 detailTextLabel
@property (nonatomic, readonly, retain) UILabel *detailTextLabel;

//4.2.3 imageView
@property (nonatomic, readonly, retain) UIImageView *imageView;



4.3 UITableViewCell的UITableViewCellStyle属性

UITableViewCellStyle用于决定使用contentView的哪些子视图,以及这些子视图在contentView中的位置

 

UITableViewCellStyle的值子视图样式
UITableViewCellStyleDefault191347_PlHu_1032974.png
UITableViewCellStyleSubtitle191347_vAAu_1032974.png
UITableViewCellStyleValue1191347_2nbd_1032974.png
UITableViewCellStyleValue2191346_Sw2K_1032974.png




五 界面简单实现

提供car类,创建car类的数组,用于展示.

5.1 代码
--car类
@interface GMCar : NSObject
@property (nonatomic, copy) NSString   *title;
@property (nonatomic, copy) NSString   *desc;
@property (nonatomic, strong) NSArray *subCars;
@end


#import "ViewController.h"
#import "GMCar.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *array;
@end

@implementation ViewController


- (void)viewDidLoad {
    //1.UITableView 设置
    //1.1 设置数据代理
    self.tableView.dataSource = self;
    //1.2 设置frame
    self.tableView.frame = self.view.frame;
    //1.3 设置代理
    self.tableView.delegate = self;
    
    //2.UITableView加入veiw
    [self.view addSubview:self.tableView];
}



#pragma mark - 数据加载
/**
 *  调用数据源以下方法获取一共有多少组数据
 *
 *  @param tableView <#tableView description#>
 *
 *  @return <#return value description#>
 */
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.array.count;
}

/**
 *  调用数据源方法获取每一组有多少行数据
 *
 *  @param tableView <#tableView description#>
 *  @param section   <#section description#>
 *
 *  @return <#return value description#>
 */
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    GMCar *car = self.array[section];
    return car.subCars.count;
}

/**
 *  调用数据源方法获取每一行显示什么内容
 *
 *  @param tableView <#tableView description#>
 *  @param indexPath <#indexPath description#>
 *
 *  @return <#return value description#>
 */
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1.创建cell
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    
    //1.1 设置accessoryType
    //    UITableViewCellAccessoryNone,                   // don't show any accessory view
    //    UITableViewCellAccessoryDisclosureIndicator,    // regular chevron. doesn't track
    //    UITableViewCellAccessoryDetailDisclosureButton, // info button w/ chevron. tracks
    //    UITableViewCellAccessoryCheckmark,              // checkmark. doesn't track
    //    UITableViewCellAccessoryDetailButton NS_ENUM_AVAILABLE_IOS(7_0) // info button. tracks
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    
    //2.取出数据
    GMCar *car = self.array[indexPath.section];
    NSString *name =car.subCars[indexPath.row];
    
    //3.设置数据
    cell.textLabel.text =name;
    
    return cell;
}

/**
 *  section组显示标题
 *
 *  @param tableView <#tableView description#>
 *  @param section   <#section description#>
 *
 *  @return <#return value description#>
 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    GMCar *car = self.array[section];
    return car.title;
}

/**
 *  section组显示描述
 *
 *  @param tableView <#tableView description#>
 *  @param section   <#section description#>
 *
 *  @return <#return value description#>
 */
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    GMCar *car = self.array[section];
    return car.desc;
}




#pragma mark - UITableViewDelegate
/**
 *  table 每一行的高度
 *
 *  @param tableView <#tableView description#>
 *  @param indexPath <#indexPath description#>
 *
 *  @return <#return value description#>
 */
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 20.0;
}

/**
 *  section组Header的高度
 *
 *  @param tableView <#tableView description#>
 *  @param section   <#section description#>
 *
 *  @return <#return value description#>
 */
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    NSLog(@"height");
    return 30.0;
}

/**
 *  section组Footer的高度
 *
 *  @param tableView <#tableView description#>
 *  @param section   <#section description#>
 *
 *  @return <#return value description#>
 */
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 30.0;
}






#pragma mark - 懒加载
-(UITableView *)tableView
{
    if (!_tableView) {
        //UITableViewStyleGrouped样式
        _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
        //UITableViewStylePlain 样式
        //_tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    }
    return _tableView;
}


-(NSArray *)array
{
    if (!_array) {
        _array = [[NSArray alloc]init];
        GMCar *c1 = [[GMCar alloc] init];
        c1.title = @"德国品牌";
        c1.desc = @"世界一流品牌";
        c1.subCars = @[@"奥迪" , @"宝马"];
        
        GMCar *c2 = [[GMCar alloc] init];
        c2.title = @"日本品牌";
        c2.desc = @"实用价值高";
        c2.subCars = @[@"丰田" , @"本田"];
        
        GMCar *c3 = [[GMCar alloc] init];
        c3.title = @"欧美品牌";
        c3.desc = @"高逼格";
        c3.subCars = @[@"劳斯莱斯" , @"布加迪", @"兰博基尼"];
        _array = @[c1, c3, c2];
    }
    return _array;
}


#pragma mark - 隐藏状态栏
- (BOOL)prefersStatusBarHidden
{
    return YES;
}
@end


5.2 展示

UITableViewStylePlain 样式                      UITableViewStyleGrouped 样式                  

225334_wjb1_1032974.png       225334_qmD1_1032974.png   


UITableViewCell的accessoryType值为UITableViewCellAccessoryDetailDisclosureButton

   225706_uZhL_1032974.png


转载于:https://my.oschina.net/wolx/blog/366709

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值