UITableView總結

性能優化

/**
 *  每當有一個cell要進入視野範圍內,就會調用一次
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"wine";

    // 1.先去緩存池中查找可循環利用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 2.若是緩存池中沒有可循環利用的cell
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        // 儘可能將cell的初始化設置,放在這個代碼塊中
        // 若是這個設置是全部cell都要保持一致的,就能夠放在這個代碼塊中
        cell.textLabel.font = [UIFont systemFontOfSize:30];
        cell.backgroundColor = [UIColor redColor];
    }

    // 3.設置數據
    cell.textLabel.text = [NSString stringWithFormat:@"%zd行的數據", indexPath.row];
    if (indexPath.row % 2 == 0) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

索引條

/**
 *  返回每一組的索引標題(數組中放的是字符串)
 */
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return [self.carGroups valueForKeyPath:@"title"];
}

如何讓tableView展現數據

  • 設置數據源對象
self.tableView.dataSource = self;
  • 數據源對象要遵照協議
@interface ViewController () <UITableViewDataSource>

@end
  • 實現數據源方法
// 多少組數據
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

// 每一組有多少行數據
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

// 每一行顯示什麼內容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

// 每一組的頭部
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

// 每一組的尾部
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

tableView的常見設置

tableview的點擊後,點擊效果馬上消失html

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

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

樣式設置:數組

// 設置每一行cell的高度
self.tableView.rowHeight = 100;

// 設置每一組頭部的高度
self.tableView.sectionHeaderHeight = 50;

// 設置每一組尾部的高度
self.tableView.sectionFooterHeight = 50;

// 設置分割線顏色
self.tableView.separatorColor = [UIColor redColor];
// 設置分割線樣式
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// 設置表頭控件
self.tableView.tableHeaderView = [[UISwitch alloc] init];
// 設置表尾控件
self.tableView.tableFooterView = [UIButton buttonWithType:UIButtonTypeContactAdd];

// 設置右邊索引文字的顏色
self.tableView.sectionIndexColor = [UIColor redColor];
// 設置右邊索引文字的背景色
self.tableView.sectionIndexBackgroundColor = [UIColor blackColor];

tableViewCell的常見設置

// 設置右邊的指示樣式
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

// 設置右邊的指示控件
cell.accessoryView = [[UISwitch alloc] init];

// 設置cell的選中樣式
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// backgroundView優先級 > backgroundColor

// 設置背景色
cell.backgroundColor = [UIColor redColor];

// 設置背景view
UIView *bg = [[UIView alloc] init];
bg.backgroundColor = [UIColor blueColor];
cell.backgroundView = bg;

// 設置選中的背景view
UIView *selectedBg = [[UIView alloc] init];
selectedBg.backgroundColor = [UIColor purpleColor];
cell.selectedBackgroundView = selectedBg;

cell的循環利用

  • 傳統的寫法
/**
 *  每當有一個cell要進入視野範圍內,就會調用一次
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"wine";

    // 1.先去緩存池中查找可循環利用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 2.若是緩存池中沒有可循環利用的cell
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }

    // 3.設置數據
    cell.textLabel.text = [NSString stringWithFormat:@"%zd行的數據", indexPath.row];

    return cell;
}
  • 新的寫法(註冊cell)
NSString *ID = @"wine";

- (void)viewDidLoad {
    [super viewDidLoad];

    // 註冊某個重用標識 對應的 Cell類型
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.先去緩存池中查找可循環利用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 2.設置數據
    cell.textLabel.text = [NSString stringWithFormat:@"%zd行的數據", indexPath.row];

    return cell;
}

推薦一個比較好的總結:http://www.open-open.com/lib/view/open1430008922468.html緩存

相關文章
相關標籤/搜索