溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

UIKit框架(20)表格視圖UITableView

發(fā)布時(shí)間:2020-07-25 15:38:30 來源:網(wǎng)絡(luò) 閱讀:496 作者:ymanmeng123 欄目:移動(dòng)開發(fā)

UITableView是UIKit中最常用的一種視圖,是UIScrollView的子類

本篇文章介紹 UITableView的基本使用,包括:

    UITableView的數(shù)據(jù)源驅(qū)動(dòng)

    各種數(shù)據(jù)源、代理方法

    單元格的重用機(jī)制

    數(shù)據(jù)的刷新

    ...




  • UITableView的樣式

創(chuàng)建時(shí)需要指定樣式:

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
@property(nonatomic, readonly) UITableViewStyle style
typedef enum {
     UITableViewStylePlain,
     UITableViewStyleGrouped
} UITableViewStyle;

    UIKit框架(20)表格視圖UITableView


  • UITableView中的內(nèi)容

表格視圖中可以包含多個(gè)組

每個(gè)組中又可以包含多個(gè)單元格(cell)

每個(gè)組上面的header視圖

每個(gè)組下面的footer視圖


這些屬性的賦值:使用數(shù)據(jù)源和代理驅(qū)動(dòng)


  • UITableView的數(shù)據(jù)源驅(qū)動(dòng)

UITableView包含兩個(gè)代理:

@property(nonatomic, assign) id< UITableViewDelegate > delegate  //代理
@property(nonatomic, assign) id< UITableViewDataSource > dataSource //數(shù)據(jù)源

     數(shù)據(jù)源可以使用代理設(shè)計(jì)模式,其功能屬于代理的第三種應(yīng)用,為自身屬性賦值


重要的數(shù)據(jù)源方法:

     表格視圖中應(yīng)當(dāng)包含多少個(gè)組,默認(rèn)為1

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

     表格視圖中指定組中應(yīng)當(dāng)包含多少個(gè)cell,必須實(shí)現(xiàn)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

     表格視圖中指定組及行的cell視圖,必須實(shí)現(xiàn)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

     表格視圖中的cell視圖在將要顯示時(shí)自動(dòng)調(diào)用,應(yīng)將數(shù)據(jù)綁定的代碼放在這里

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

    一般來說,這四個(gè)數(shù)據(jù)源方法,是必須要實(shí)現(xiàn)的(第一個(gè)不實(shí)現(xiàn)默認(rèn)為一個(gè)section)


如:

//當(dāng)前控制器遵循數(shù)據(jù)源、代理協(xié)議
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
//當(dāng)前控制器成為tableView的數(shù)據(jù)源和代理
self.tableView.dataSource = self;
self.tableView.delegate = self;
//四個(gè)數(shù)據(jù)源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;//三個(gè)section
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return section+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"111"];
    return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.textLabel.text = [NSString stringWithFormate:@"Section:%ld Row:%ld", indexPath.section, indexPath.row];
}


  • UITableView的行高

兩種方式:

1)統(tǒng)一的高度通過UITableView對(duì)象的rowHeight屬性設(shè)定

@property(nonatomic) CGFloat rowHeight

2)也可以通過實(shí)現(xiàn)tableView的代理方法,返回每一行的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath


  • UITableView的其他數(shù)據(jù)源、代理方法

行被點(diǎn)擊(選擇)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath

section的右側(cè)索引

- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView

section的header和footer

//header、footer上的文字
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
//header、footer為自定義的視圖
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
//header、footer的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

編輯模式

//返回每個(gè)cell的編輯狀態(tài)的模式(刪除、插入)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
//響應(yīng)點(diǎn)擊編輯按鈕時(shí)的動(dòng)作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath

    編輯模式有兩種方式進(jìn)入:

    1)滑動(dòng)單元格,當(dāng)前單元格進(jìn)入編輯模式

    2)修改UITableView對(duì)象的editing屬性

@property(nonatomic, getter=isEditing) BOOL editing //編輯模式使能
- (void)setEditing:(BOOL)editing animated:(BOOL)animate



  • 單元格的重用機(jī)制

為了有效的利用內(nèi)存,UITableView使用了重用機(jī)制管理其內(nèi)部顯示的所有cell

     當(dāng)一個(gè)cell離開了屏幕范圍時(shí),會(huì)將其從tableView內(nèi)部移除并放到一個(gè)緩存隊(duì)列中存儲(chǔ)

     當(dāng)一個(gè)cell將要顯示時(shí),如果隊(duì)列中存在cell,則直接重用該cell,如果沒有則創(chuàng)建新的

這個(gè)隊(duì)列稱之為“重用隊(duì)列”,這種管理內(nèi)部視圖的方式稱之為“重用機(jī)制”

 

重用ID:

     一個(gè)tableView中可以存在多種不同樣式的cell,引入重用ID以區(qū)分不同的樣式

     即:從重用隊(duì)列取cell時(shí),要按照指定的ID取出,創(chuàng)建cell時(shí)要設(shè)置其ID


從重用隊(duì)列取出cell的方法:(UITableView)

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier

將上面返回cell的數(shù)據(jù)源方法修改為:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *cellID = @"cell";
    UITableViewCell * cell = [tableView dequeueResuableCellWithIdentifier:cellID];
    if ( cell == nil ) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"111"];
    }
    return cell;
}


  • UITableView數(shù)據(jù)的重新加載

在實(shí)際開發(fā)中,觸發(fā)了某些事件(如網(wǎng)絡(luò)獲取到更多數(shù)據(jù)、用戶請(qǐng)求刷新等),要求UITableView刷新顯示所有數(shù)據(jù)

刷新的方法如下:

- (void)reloadData
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
- (void)reloadSectionIndexTitles //重新加載右側(cè)的索引

    這些方法將重新調(diào)用全部或部分的數(shù)據(jù)源、代理方法,重新展示數(shù)據(jù)


重新加載數(shù)據(jù)的通常做法是:

    控制器實(shí)現(xiàn)UITableView的數(shù)據(jù)源和代理方法,并管理著需要顯示的數(shù)據(jù)模型(數(shù)組)

    當(dāng)需要刷新數(shù)據(jù)時(shí),控制器修改數(shù)據(jù)模型(數(shù)組),然后調(diào)用UITableView的刷新方法

    即:修改模型 --> reloadData 



  • UITableView的其他的屬性及方法

UITableView的背景視圖(通常設(shè)置一個(gè)UIImageView對(duì)象)

@property(nonatomic, readwrite, retain) UIView *backgroundView

滾動(dòng)到指定的單元格

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated

單元格之間的分割樣式及顏色

@property(nonatomic) UITableViewCellSeparatorStyle separatorStyle
@property(nonatomic, retain) UIColor *separatorColor

單元格與indexPath的互相獲取

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath 
- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell

獲得當(dāng)前顯示的單元格

- (NSArray *)visibleCells //獲得所有可見的cell
- (NSArray *)indexPathsForVisibleRows //獲得所有可見cell的indexPath

選擇相關(guān)設(shè)置

@property(nonatomic) BOOL allowsSelection //cell選擇的使能
@property(nonatomic) BOOL allowsMultipleSelection //多選使能
- (NSIndexPath *)indexPathForSelectedRow //當(dāng)前被選擇的cell的indexPath
- (NSArray *)indexPathsForSelectedRows //多選時(shí)
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animatedscrollPosition:(UITableViewScrollPosition)scrollPosition //選擇指定行
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated //反選




向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI