前言
在iOS開發(fā)過程中霞揉,UITableView可以說是實(shí)用最頻繁的UIKit控件了膀捷,在這里我會先給出純代碼方法是用UITableView,以后會添加上使用StoryBoard的方法。希望能幫助一些iOS開發(fā)入門者(經(jīng)驗(yàn)之談黑忱,不足之處也期待有高手指教)
正文
UITableView采用的是iOS開發(fā)中常用的代理模式,即將控件需要使用的方法函數(shù)寄托給另一方讓其代理完成勒魔。UITableView使用了UITableViewDelegate 和 UITableViewDataSource 兩個代理方法甫煞。我們在使用UITableView過程中可以將這兩個代理交給任何NSObject讓其代理執(zhí)行。廢話不多說冠绢,上代碼抚吠。
UITableViewDataSource 包括以下方法
兩個required(必要)方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
//第一個方法是返回一個Section中有多少行,一個Section類似于一組
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
//第二個方法是返回一個UITableViewCell弟胀,每一行對應(yīng)一個Cell楷力,Cell可自定義喊式,還有重用Cell等問題
//indexPath包括section和row信息,對應(yīng)第section的組和這個組中的第row行
//通過[indexPath row]和[indexPath section]獲得
其它optional(非必要)方法:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
//這里返回的是TableView中Section個數(shù)弥雹,默認(rèn)為一個
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
//返回一個Section頂上的文字 根據(jù)Section不同文字可以不同垃帅,通常用來歸類顯示
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
//同理,返回一個Section底端的文字
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
//能否編輯一個indexPath對應(yīng)的cell
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
//能否移動一個indexPath對應(yīng)的cell
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView __TVOS_PROHIBITED;
//返回一個數(shù)組剪勿,數(shù)組中應(yīng)該包含的是tableView的索引信息贸诚,聯(lián)想通訊錄
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index __TVOS_PROHIBITED;
//設(shè)置哪一個section對應(yīng)哪一個索引信息,聯(lián)想通訊錄
- (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(非必要)方法酱固,最常用的是點(diǎn)擊cell的反應(yīng),和編輯移動cell等头朱。今天就暫且先不羅列运悲。
說了些理論方法,我簡單寫一個例子展示如何具體使用项钮。
程序都在ViewController中實(shí)現(xiàn)班眯,有.h和.m兩個文件
————————————————————————
ViewController.h 文件:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
ViewController.m 文件:
#import "ViewController.h"
//在這里設(shè)置代理
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *mainTableView;//我們創(chuàng)建的UITableView
@property (nonatomic, strong) NSMutableArray *mainTableViewDataArray;//存放UITableView的數(shù)據(jù)
@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;
//將我們創(chuàng)建的UITableView的代理交給self 也就是ViewController
//如果沒有這一步相當(dāng)于mainTableView沒有人代理,也就不會實(shí)現(xiàn)代理方法
[self.view addSubview:_mainTableView];
}
return _mainTableView;
}
//初始化數(shù)據(jù)烁巫,這里是本地死的測試數(shù)據(jù)
- (void)initLocalData{
_mainTableViewDataArray = [[NSMutableArray alloc]initWithObjects:@"hello",@"world", nil];
[_mainTableView reloadData];//重新刷新tableView數(shù)據(jù)
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _mainTableViewDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//使用cell重用機(jī)制
static NSString *cellIdentifier = @"cellIdentifier";//設(shè)置cell重用標(biāo)示
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//根據(jù)標(biāo)示去找cell署隘,如果有現(xiàn)成的就用現(xiàn)成的
if (!cell) {
//沒有現(xiàn)成的cell的時候:
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [_mainTableViewDataArray objectAtIndex:[indexPath row]];
//給cell設(shè)置內(nèi)容 從之前設(shè)置的數(shù)據(jù)數(shù)組中拿數(shù)據(jù)
return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//這里是cell的點(diǎn)擊事件 點(diǎn)擊了cell便觸發(fā)這個函數(shù)
NSLog(@"點(diǎn)擊了cell");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
結(jié)果:
結(jié)果.png
先寫到這里,由于UITableView內(nèi)容還挺多亚隙,以后會一一增加磁餐,希望對讀者有用。
3.17 更新:
很多時候不想空的cell還顯示橫線阿弃,設(shè)置UITableView 的 footerView屬性可以達(dá)到目的:
UITableView *tableView = [[UITableView alloc]init];
tableView.tableFooterView = [[UIView alloc]init];