iOS開發之初識UITableView

初識
android

在iOS中,要實現表格數據展現,最經常使用的作法就是使用UITableView,UITableView繼承自UIScrollView,所以支持垂直滾動,並且性能極佳。她有兩種樣式:UITableViewStylePlain和UITableViewStyleGrouped,前者其實就是android中的ListView或者RecyclerView,然後者樣式在android中是須要經過xml建立佈局的,總的說來這玩意功能比較全。緩存

使用
性能優化

一、設置數據源:要用UITableVIew顯示數據,就要給她設置一個數據源,UITableView會向數據源查詢一共有多少行數據以及每一行顯示什麼數據等,沒有設置數據源的UITableView只是個空殼,凡是遵照UITableViewDataSource協議的OC對象,均可以是UITableView的數據源。佈局

二、顯示數據的過程:性能

/**
 *  告訴tableView一共有多少組數據
 */
- (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組的頭部標題
 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
/**
 *  告訴tableView第section組的尾部標題
 */
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section


固然,遵照UITableViewDataSource協議後,必須實現的方法只有兩個:優化

@required

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

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

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


三、Cell的重用原理:當滾動列表時,部分UITableViewCell會移出窗口,UITableView會將窗口外的UITableViewCell放入一個對象池中,等待重用。當UITableView要求dataSource返回UITableViewCell時,dataSource會先查看這個對象池,若是池中有未使用的UITableViewCell,dataSource會用新的數據配置這個UITableViewCell,而後返回給UITableView,從新顯示到窗口中,從而避免建立新對象。
ui

四、還有一個很是重要的問題:有時候須要自定義UITableViewCell(用一個子類繼承UITableViewCell),並且每一行用的不必定是同一種UITableViewCell,因此一個UITableView可能擁有不一樣類型的UITableViewCell,對象池中也會有不少不一樣類型的UITableViewCell,那麼UITableView在重用UITableViewCell時可能會獲得錯誤類型的UITableViewCell。spa

解決方案:UITableViewCell有個NSString *reuseIdentifier屬性,能夠在初始化UITableViewCell的時候傳入一個特定的字符串標識來設置reuseIdentifier(通常用UITableViewCell的類名)。當UITableView要求dataSource返回UITableViewCell時,先經過一個字符串標識到對象池中查找對應類型的UITableViewCell對象,若是有,就重用,若是沒有,就傳入這個字符串標識來初始化一個UITableViewCell對象。code


UITableView性能優化orm

 cell的循環利用方式1

/**
 *  何時調用:每當有一個cell進入視野範圍內就會調用
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 0.重用標識
    // 被static修飾的局部變量:只會初始化一次,在整個程序運行過程當中,只有一分內存
    static NSString *ID = @"cell";
    // 1.先根據cell的標識去緩存池中查找可循環利用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    // 2.若是cell爲nil(緩存池找不到對應的cell)
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    // 3.覆蓋數據
    cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row];
    return cell;
}

cell的循環利用方式2 (定義一個全局變量)

// 定義重用標識
NSString *ID = @"cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    // 註冊某個標識對應的cell類型
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.去緩存池中查找cell(這時候獲得的cell一定不爲nil)
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    // 2.覆蓋數據
    cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row];
    //在數據源方法中返回cell
    return cell;
}

cell的循環利用方式3

、在storyboard中設置UITableView的Dynamic Prototypes Cell

、設置cell的重用標識

、在代碼中利用重用標識獲取cell

// 0.重用標識
// 被static修飾的局部變量:只會初始化一次,在整個程序運行過程當中,只有一分內存
static NSString *ID = @"cell";
// 1.先根據cell的標識去緩存池中查找可循環利用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 2.覆蓋數據
cell.textLabel.text = [NSString stringWithFormat:@"cell - %zd", indexPath.row];
return cell;


UITableView的常見設置

// 分割線顏色
self.tableView.separatorColor = [UIColor redColor];
// 隱藏分割線
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// tableView有數據的時候才須要分割線
// 開發小技巧:快速取消分割線
 self.tableView.tableFooterView = [[UIView alloc] init];


UITableViewCell的常見設置

// 取消選中的樣式(經常使用) 讓當前 cell 按下無反應
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// 設置選中的背景色
UIView *selectedBackgroundView = [[UIView alloc] init];
selectedBackgroundView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = selectedBackgroundView;
// 設置默認的背景色
cell.backgroundColor = [UIColor blueColor];
// 設置默認的背景色
UIView *backgroundView = [[UIView alloc] init];
backgroundView.backgroundColor = [UIColor greenColor];
cell.backgroundView = backgroundView;
// backgroundView的優先級 > backgroundColor
// 設置指示器
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.accessoryView = [[UISwitch alloc] init];
相關文章
相關標籤/搜索