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<NSString *> *)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 <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

ViewController.m 文件:

#import "ViewController.h"

//在这里设置代理
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@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

结果:

结果.png

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


3.17 更新:

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

UITableView *tableView = [[UITableView alloc]init];
tableView.tableFooterView = [[UIView alloc]init];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,391评论 30 472
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,427评论 4 61
  • Swift版本点击这里欢迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh阅读 25,702评论 7 249
  • 看到美女,明明心跳。 假模假样,斜眼而过。 看到帅哥,明明心动。 昂首阔步,面红而行。 一张钞票,明知是假。 去买...
    泊宁赵阅读 3,730评论 8 8
  • 今天心情不错,虽然自从生了二宝以后就有一种浑身放松,握拳蓄力想大干一场的冲动,每天精神满满处理各种事情,和大宝那时...
    毛毛咻咻萌萌阅读 1,216评论 0 0

友情链接更多精彩内容