開發筆記-tableView展現數據常見設置

  1. 如何讓tableView展現數據

    • 設置數據源對象
  2. 
    
    self.tableView.dataSource = self;
    • 數據源對象要遵照協議
  3. 
    
    @interface ViewController () <UITableViewDataSource>
    
    @end
    • 實現數據源方法
  4. 
    
    // 多少組數據
    - (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的常見設置


    // 設置每一行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];
  5.  
    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;

    代理方法
/**
 *  當選中一行的時候調用(點擊)
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    XMGWine *wine = self.wineArray[indexPath.row];
//    NSLog(@"點擊了:%@", wine.name);
    NSLog(@"選中了:%zd", indexPath.row);
}

/**
 *  當取消選中一行的時候調用
 */
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"取消選中了:%zd", indexPath.row);
}
/**
 *  在每一組的頭部設置控件
 */
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIButton buttonWithType:UIButtonTypeInfoDark];
}
/**
 *  在每一組的尾部設置控件
*/
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [[UISwitch alloc] init];
}
/**
 *  能夠判斷不一樣組設置不一樣的高度
*/
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) return 20;
    if (section == 1) return 50;
}
 
/**
 *  返回每一個cell的高度(設置不一樣行不一樣高度)
 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row % 2 == 0) {
        return 50;
    } else {
        return 100;
    }
}
 
 ----Make by -LJW 轉載請註明出處--- 
相關文章
相關標籤/搜索