您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 程式設計技術

iOS 開發:UITableView 滾動優化與自訂 Cell

編輯:iOS 程式設計技術

iOS 開發中 UITableView(表視圖)的滾動優化及自訂 Cell

iOS 開發中,UITableView 是非常常用的一個控制項。我們平時在手機上看到的聯繫人列表、微信好友列表等都是透過 UITableView 實現的。UITableView 的每一行是一個 cell,當 cell 數量特別大時,由於每次都需 alloc 分配記憶體並初始化,會導致 App 運行不流暢。所以可以使用蘋果提供的幾個方法進行優化,我把這個過程記錄下來供日後查閱。

既然說到優化,那就從沒有優化的狀態談起:先用最基本的流程實現一個 UITableView,再談優化與自訂。

本文正文主要分三步:

一、使用最基本的流程實現一個表視圖。

二、透過滾動優化 cell 的建立以節約記憶體。

三、自訂表視圖中 cell 的樣式。

 

 

一、使用最基本的流程實現一個表視圖:

UITableView 的每一行是一個 cell,所以我們先建立一個 UITableView,然後在 datasource 代理方法中返回 cell 的個數並建立 cell:

1、建立 UITableView 控制項 myTableView,設為全域變數、設置代理、添加到當前 view:

#import "ViewController.h"

@interface ViewController () {
    UITableView *myTableView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 414, 600)];
    myTableView.backgroundColor = [UIColor lightGrayColor];
    myTableView.delegate = self;
    myTableView.dataSource = self;
    [self.view addSubview:myTableView];
}

 

2、在頭檔中添加代理:

//
//  ViewController.h
//  UITableView列表
//
#import <UIKit/UIKit.h>

// 添加代理 UITableViewDataSource 和 UITableViewDelegate
@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@end

 

3、進入 UITableViewDataSource 找到代理方法並粘貼到 .m 檔中,實現方法。其中:

- (NSInteger)tableView:numberOfRowsInSection: 返回一個數字,是 cell 的個數;

- (UITableViewCell *)tableView:cellForRowAtIndexPath: 返回的是 UITableViewCell,所以我們在這個函數中建立 cell 並返回,這樣就實現了代理方法。

具體程式碼:

// myTableView 的行數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 建立 UITableViewCell
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    // UITableViewCell 自帶 textLabel,indexPath.row 為行數
    cell.textLabel.text = [NSString stringWithFormat:@"這是第%ld行", indexPath.row];
    return cell;
}

 

以上程式碼第 9 行建立並初始化了 UITableViewCell,第 11 行給 cell 自帶的 textLabel 設置文字。這樣的做法是:需要多少個 cell 就 alloc 多少次,如果需要的 cell 個數太多,會導致下拉時非常卡。但在需要展示的資料量很小、也就是 cell 不多的時候,可以使用這種方式。

透過這種方式實現的表視圖模擬如下:

 

 

二、透過優化 cell 的建立使之節約記憶體。

滾動優化的原理是:如果這個螢幕能顯示 10 個 cell,那就建立 11 個 cell,不管有多少資料要顯示,就用這 11 個 cell。當使用者上拉或下拉時,上面消失的 cell 將重新裝入要顯示的資料,然後顯示在螢幕上,相當於總有一個在緩衝等待顯示。透過這樣的方式減少了 cell 的 alloc 次數,進而降低了記憶體開銷,提高了系統效能。

要實現以上思路,只需把「一」中建立 cell 的程式碼換成以下語句:

// 2、使用佇列池優化方式建立 cell
static NSString *cellid = @"cell"; // 一個固定的標識
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
}

 

三、自訂表視圖中 cell 樣式:

1、先新建一個繼承自 UITableViewCell 的類,我這裡建立的名稱是 myTableViewCell,建立時勾選建立 nib 檔。

2、打開建立好的 nib 檔,裡面已經有一個 cell 了,我們採用拖拽的辦法給這個 cell 加入一個 UIImageView,並放置一張圖片(這裡使用一張黃色純色圖片),然後在 cell 左邊拖入一個 button、右邊拖入一個 label,完成後如下圖:

 

3、在 myTableViewCell.h 中定義兩條語句:

@property(nonatomic, strong) IBOutlet UIButton *mybutton;
@property(nonatomic, strong) IBOutlet UILabel *mylabel;

 寫完這兩句之後就可以在 nib 中拖線連接關係了,這個過程是最基本的,這裡不再贅述。

 

4、在 ViewController.m 中完成 App 的主要功能,也就是表視圖的實現:

//
//  ViewController.m
//  UITableView使用nib拖動自定義
//
#import "ViewController.h"
#import "myTableViewCell.h"

@interface ViewController () {
    UITableView *myTableView;
    NSArray *contentArray;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 414, 500)];
    myTableView.delegate = self;
    myTableView.dataSource = self;
    [self.view addSubview:myTableView];
    // 用來為 cell 的 mylabel 賦值的陣列
    contentArray = @[@"one", @"two", @"three", @"four", @"five", @"six", @"seven", @"eight", @"nine", @"ten"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // 設置 cell 的數量為陣列的元素個數
    return contentArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 優化方式建立 cell
    static NSString *cellid = @"cell";
    myTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell == nil) {
        // 使用繪製好的 nib 為 cell 賦值
        cell = (myTableViewCell *)[[[NSBundle mainBundle] loadNibNamed:@"myTableViewCell" owner:self options:nil] objectAtIndex:0];
        // 為 cell 上按鈕設置 tag,為了從 cell 上的多個控制項中區分出某個 button
        cell.mybutton.tag = 1000;
        // 為 cell 上的 button 添加方法
        [cell.mybutton addTarget:self action:@selector(haha:) forControlEvents:UIControlEventTouchUpInside];
        // 使用陣列的內容為 cell 賦值
        cell.mylabel.text = [contentArray objectAtIndex:[indexPath row]];
    }
    return cell;
}

// 拿到 cell,然後根據 tag 拿到 button 並輸出 button 上的文字
-(void)haha:(UIButton *)sender {
    // button 在 cell 的 content 上,所以透過以下方法獲取
    myTableViewCell *cell = (myTableViewCell *)[[sender superview] superview];
    UIButton *mybutton = (UIButton *)[cell viewWithTag:1000];
    NSLog(@"%@", mybutton.titleLabel.text);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end

 模擬效果如下:

  以上。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved