UI基礎(chǔ)9 UITableView

UITableView

UITableView的兩種樣式

  • UITableViewStylePlain
  • UITableViewStyleGrouped

如何展示數(shù)據(jù)

  • UITableView需要一個(gè)數(shù)據(jù)源(dataSource)來(lái)顯示數(shù)據(jù)
  • UITableView會(huì)向數(shù)據(jù)源查詢一共有多少行數(shù)據(jù)以及每一行顯示什么數(shù)據(jù)等
  • 沒(méi)有設(shè)置數(shù)據(jù)源的UITableView只是個(gè)空殼
  • 凡是遵守UITableViewDataSource協(xié)議的OC對(duì)象,都可以是UITableView的數(shù)據(jù)源

tableView展示數(shù)據(jù)的過(guò)程

  • 調(diào)用數(shù)據(jù)源的下面方法得知一共有多少組數(shù)據(jù)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
  • 調(diào)用數(shù)據(jù)源的下面方法得知每一組有多少行數(shù)據(jù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
  • 調(diào)用數(shù)據(jù)源的下面方法得知每一行顯示什么內(nèi)容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
  • tableView如何顯示數(shù)據(jù)
    • 設(shè)置dataSource數(shù)據(jù)源
    • 數(shù)據(jù)源要遵守UITableViewDataSource協(xié)議
    • 數(shù)據(jù)源要實(shí)現(xiàn)協(xié)議中的某些方法
/**
 *  告訴tableView一共有多少組數(shù)據(jù)
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

/**
 *  告訴tableView第section組有多少行
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

/**
 *  告訴tableView第indexPath行顯示怎樣的cell
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

/**
 *  告訴tableView第section組的頭部標(biāo)題
 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

/**
 *  告訴tableView第section組的尾部標(biāo)題
 */
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

tableView性能優(yōu)化 - cell的循環(huán)利用方式1

/**
 *  什么時(shí)候調(diào)用:每當(dāng)有一個(gè)cell進(jìn)入視野范圍內(nèi)就會(huì)調(diào)用
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 0.重用標(biāo)識(shí)
    // 被static修飾的局部變量:只會(huì)初始化一次,在整個(gè)程序運(yùn)行過(guò)程中,只有一份內(nèi)存
    static NSString *ID = @"cell";

    // 1.先根據(jù)cell的標(biāo)識(shí)去緩存池中查找可循環(huán)利用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 2.如果cell為nil(緩存池找不到對(duì)應(yīng)的cell)
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }

    // 3.覆蓋數(shù)據(jù)
    cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row];

    return cell;
}

tableView性能優(yōu)化 - cell的循環(huán)利用方式2

  • 定義一個(gè)全局變量
// 定義重用標(biāo)識(shí)
NSString *ID = @"cell";
  • 注冊(cè)某個(gè)標(biāo)識(shí)對(duì)應(yīng)的cell類型
// 在這個(gè)方法中注冊(cè)cell
- (void)viewDidLoad {
    [super viewDidLoad];

    // 注冊(cè)某個(gè)標(biāo)識(shí)對(duì)應(yīng)的cell類型
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
}
  • 在數(shù)據(jù)源方法中返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.去緩存池中查找cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 2.覆蓋數(shù)據(jù)
    cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row];

    return cell;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容