• UITableView的基本用法
  • 一实例化和基本属性
  • 实例化
  • 基本属性
  • 二UITableView的两种内置样式
  • UITableViewStylePlain
  • UITableViewStyleGrouped
  • 三数据源dataSource
  • 四代理delegate
  • 五UITableView的数据源协议示意图
  • 六UITableView的代理协议示意图
  • 七基本方法
  • 八省市示例
  • 代码
  • 图示
  • 九商品信息
  • 代码
  • 图示


UITableView的基本用法

一、实例化和基本属性

1.实例化

- (instancetype nonnull)init
- initWithCoder:(nonnull NSCoder *)
- (instancetype nonnull)initWithFrame:(CGRect)aRect
- (instancetype nonnull)initWithFrame:(CGRect)frame  style:(UITableViewStyle)style

2.基本属性

分隔线属性
–separatorStyle 分隔线样式
–separatorColor 分隔线颜色
选中属性
–allowsSelection 允许选中
–allowsMultipleSelection 允许多选
行数
–indexPathsForSelectedRows 当前选中行数
–indexPathsForVisibleRows 当前可见行数
背景
–backgroundView  背景视图
–selectedBackgroundView 选中时的背景视图
另外,UITableViewCell的selectionStyle属性可设置被选中时的背景颜色:
–UITableViewCellSelectionStyleNone  没有颜色
–UITableViewCellSelectionStyleBlue  蓝色(默认)
–UITableViewCellSelectionStyleGray  灰色

二、UITableView的两种内置样式

1.UITableViewStylePlain

iOS collectionview显示两行 iosuitableview_UIPickerVi

2.UITableViewStyleGrouped

iOS collectionview显示两行 iosuitableview_UITableVie_02

三、数据源(dataSource)

UITableView需要一个数据源(dataSource)来显示数据,UITableView会向数据源查询一共有多少行数据以及每一行显示什么数据等。没有设置数据源的UITableView只是个空壳。凡是遵守UITableViewDataSource协议的OC对象,都可以是UITableView的数据源

四、代理(delegate)

通常都要为UITableView设置代理对象(delegate),以便在UITableView触发一下事件时做出相应的处理,比如选中了某一行。凡是遵守了UITableViewDelegate协议的OC对象,都可以是UITableView的代理对象
通常情况下,会让控制器充当UITableView的dataSource和delegate

五、UITableView的数据源协议示意图

iOS collectionview显示两行 iosuitableview_数据选择视图_03

六、UITableView的代理协议示意图

iOS collectionview显示两行 iosuitableview_UIPickerVi_04

七、基本方法

// 一共有多少组数据
- (NSInteger)numberOfSectionsInTableView:
// 每一组有多少行数据
- (NSInteger)tableView:  numberOfRowsInSection:
// 每一行具体显示的内容
- (UITableViewCell *)tableView:  cellForRowAtIndexPath:


// 选中了某一行
-(void)tableView:didSelectRowAtIndexPath:
// 每一行有多高
- (CGFloat)tableView:heightForRowAtIndexPath:

八、省市示例

1.代码

//
//  ViewController.m
//  03_UIView17_UITableView2_省市
//
//  Created by 杞文明 on 15/12/31.
//  Copyright © 2015年 杞文明. All rights reserved.
//

#import "ViewController.h"

@interface ViewController (){
    NSArray* _provinces;
    NSDictionary* _citeis;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableView];
    [self loadData];
}

/**创建tableview*/
-(void)createTableView{
    //1.创建UITableView
    UITableView * tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

    //2.设置数据源
    [tableView setDataSource:self];

    //3.设置代理
    [tableView setDelegate:self];

    //4.添加到view中
    [self.view addSubview:tableView];
}

/**添加数据*/
-(void)loadData{
    NSBundle * bundle = [NSBundle mainBundle];
    //加载省得数据
    _provinces = [NSArray arrayWithContentsOfFile:[bundle pathForResource:@"provinces.plist" ofType:nil]];
    //加载城市数据
    _citeis = [NSDictionary dictionaryWithContentsOfFile:[bundle pathForResource:@"cities.plist" ofType:nil]];
}

/**分组数*/
- (NSInteger)numberOfSectionsInTableView:(nonnull UITableView *)tableView{
    return _provinces.count;
}

/**每个分组的行数*/
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString * provinceName = _provinces[section];
    NSArray * cities = _citeis[provinceName];
    return cities.count;
}

/**每个单元格*/
-(nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    //1.创建cell
    UITableViewCell* cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    //2.获取对应的 省
    NSString* provinceName = _provinces[indexPath.section];

    //3.获取对应得 市
    NSArray * cities = _citeis[provinceName];
    NSString* cityStr = cities[indexPath.row];

    //4.设置值
    [cell.textLabel setText:cityStr];

    //5.返回
    return cell;
}
/**设置标题*/
-  (nullable NSString *)tableView:(nonnull UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return _provinces[section];
}

/**右侧索引的方法*/
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(nonnull UITableView *)tableView{
   return _provinces;
}

/**选中的数据*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1. 获取省的名称
    NSString *pName = _provinces[indexPath.section];
    // 2. 根据省的名称获取城市数组
    NSArray *cities = _citeis[pName];
    // 3. 从城市数组中获取对应的城市名称
    NSString *cityName = cities[indexPath.row];
    // 4. 输出用户选择的内容
    NSLog(@"%@ ~ %@", pName, cityName);
}

@end

2.图示

九、商品信息

1.代码

//
//  ViewController.m
//  03_UIView18_UITableView3_商品信息
//
//  Created by 杞文明 on 15/12/31.
//  Copyright © 2015年 杞文明. All rights reserved.
//

#import "ViewController.h"
#import "Product.h"

@interface ViewController (){
    NSMutableArray* _productList;
    UITableView * _tableView;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableView];
    [self loadData];
}

/**创建tableView*/
- (void)createTableView {
    //1.创建tableView
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds ];

    //2.设置数据源
    [_tableView setDataSource:self];

    //3.设置代理源
    [_tableView setDelegate:self];

    //4.添加到view中
    [self.view addSubview:_tableView];
}

/**加载数据*/
-(void)loadData{
    _productList = [NSMutableArray arrayWithCapacity:50];
    for (NSInteger i = 1; i <= 50 ; i++) {
        Product *pro = [[Product alloc]init];
        NSInteger nameInt = arc4random_uniform(9)+1;
        [pro setImageName:[NSString stringWithFormat:@"%03ld.png",nameInt]];
        [pro setProductName:[NSString stringWithFormat:@"商品%03ld",i]];
        [pro setDesc:[NSString stringWithFormat:@"男人必备之物---%03ld",i]];
        [_productList addObject:pro];
    }
}

/**行数*/
-(NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _productList.count;
}

/**显示的view*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    Product* pro = _productList[indexPath.row];
    UIImage* image = [UIImage imageNamed:pro.imageName];
    [cell.imageView setImage:image];
    [cell.textLabel setText:pro.productName];
    [cell.detailTextLabel setText:pro.desc];
    //有箭头
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];


    return cell;
}

/**选中某一行的时候的方法*/
- (void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    //1.取出用户选中的商品信息
    Product* product = _productList[indexPath.row];

    //2.定义UIAlertView和显示数据
    //2.1实例化UIAlertView
    UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"商品信息" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    //2.2设置样式
    [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
    //2.3设置商品名
    UITextField* textField = [alertView textFieldAtIndex:0];
    [textField setText:[product productName]];

    //3.显示UIAlertView
    [alertView show];
}

/**UIAlertView的代理*/
-(void)alertView:(nonnull UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(buttonIndex==1){//判断是否选择了 确定
        //1.取出输入的文字
        NSString * inputStr = [[alertView textFieldAtIndex:0]text];

        //2.获取我们选中的tableview中的数据
        //2.1获取选中的行对应的角标信息
        NSIndexPath* indexPath = [_tableView indexPathForSelectedRow];
        //2.2获取列表中的数据
        Product* pro = _productList[indexPath.row];

        //3.比较是否有改变
        if ([inputStr isEqual:[pro productName]]) {
            return;
        };

        //4.如果有改变,那么更新数据
        //4.1设置我们输入的文字
        [pro setProductName:inputStr];
        NSLog(@"%@",[pro productName]);
        //4.2更新表格数据
        //更新指定行的数据,更新部分数据时,支持动画效果
        [_tableView reloadRowsAtIndexPaths:[_tableView indexPathsForSelectedRows] withRowAnimation:UITableViewRowAnimationTop];
    }
}
@end

2.图示