UITableView——使用最多的控件

前言

在iOS开发过程中,UITableView可以说是实用最频繁的UIKit控件了,在这里我会先给出纯代码方法是用UITableView,以后会添加上使用StoryBoard的方法。希望能帮助一些iOS开发入门者(经验之谈,不足之处也期待有高手指教)

正文

UITableView采用的是iOS开发中常用的代理模式,即将控件需要使用的方法函数寄托给另一方让其代理完成。UITableView使用了UITableViewDelegateUITableViewDataSource 两个代理方法。我们在使用UITableView过程中可以将这两个代理交给任何NSObject让其代理执行。废话不多说,上代码。

UITableViewDataSource 包括以下方法

两个required(必要)方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
//第一个方法是返回一个Section中有多少行,一个Section类似于一组

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
//第二个方法是返回一个UITableViewCell,每一行对应一个Cell,Cell可自定义,还有重用Cell等问题
//indexPath包括section和row信息,对应第section的组和这个组中的第row行
//通过[indexPath row]和[indexPath section]获得

其它optional(非必要)方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; 
//这里返回的是TableView中Section个数,默认为一个             

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;    
//返回一个Section顶上的文字 根据Section不同文字可以不同,通常用来归类显示
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
//同理,返回一个Section底端的文字


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
//能否编辑一个indexPath对应的cell 

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
//能否移动一个indexPath对应的cell

- (nullable NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView __TVOS_PROHIBITED;       
//返回一个数组,数组中应该包含的是tableView的索引信息,联想通讯录

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index __TVOS_PROHIBITED; 
//设置哪一个section对应哪一个索引信息,联想通讯录

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
//编辑tableViewCell

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
//移动tableViewCell
UITableViewDelegate 包括38个方法,全部是optional(非必要)方法,最常用的是点击cell的反应,和编辑移动cell等。今天就暂且先不罗列。

说了些理论方法,我简单写一个例子展示如何具体使用。

程序都在ViewController中实现,有.h和.m两个文件
————————————————————————
ViewController.h 文件:

#import 

@interface ViewController : UIViewController

@end

ViewController.m 文件:

#import "ViewController.h"

//在这里设置代理
@interface ViewController ()

@property (nonatomic, strong) UITableView *mainTableView;//我们创建的UITableView
@property (nonatomic, strong) NSMutableArray *mainTableViewDataArray;//存放UITableView的数据

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self mainTableView];//懒加载tableView
    [self initLocalData];
}
- (UITableView *)mainTableView{
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-20)];
        
        //这里很重要
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        //将我们创建的UITableView的代理交给self 也就是ViewController
        //如果没有这一步相当于mainTableView没有人代理,也就不会实现代理方法
        
        [self.view addSubview:_mainTableView];
    }
    return _mainTableView;
}
//初始化数据,这里是本地死的测试数据
- (void)initLocalData{
    _mainTableViewDataArray = [[NSMutableArray alloc]initWithObjects:@"hello",@"world", nil];
    [_mainTableView reloadData];//重新刷新tableView数据
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _mainTableViewDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //使用cell重用机制
    static NSString *cellIdentifier = @"cellIdentifier";//设置cell重用标示
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    //根据标示去找cell,如果有现成的就用现成的
    if (!cell) {
        //没有现成的cell的时候:
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.textLabel.text = [_mainTableViewDataArray objectAtIndex:[indexPath row]];
    //给cell设置内容 从之前设置的数据数组中拿数据
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //这里是cell的点击事件 点击了cell便触发这个函数
    NSLog(@"点击了cell");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

结果:

UITableView——使用最多的控件_第1张图片
结果.png

先写到这里,由于UITableView内容还挺多,以后会一一增加,希望对读者有用。


3.17 更新:

很多时候不想空的cell还显示横线,设置UITableView 的 footerView属性可以达到目的:

UITableView *tableView = [[UITableView alloc]init];
tableView.tableFooterView = [[UIView alloc]init];

你可能感兴趣的:(UITableView——使用最多的控件)