开发笔记-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 转载请注明出处--- 

转载于:https://www.cnblogs.com/ljwiOS/p/5414910.html

Qt的QTableView是一个用于显示和编辑表格数据的控件。在使用QTableView时,可以使用QItemSelectionModel来获取选中的数据,并通过信号来响应选择的变化,如currentChanged、currentColumnChanged、currentRowChanged和selectionChanged。要让QTableView能够接收鼠标的移动事件,可以使用setMouseTracking(true)来设置。对于右键菜单,可以使用setContextMenuPolicy(Qt::CustomContextMenu)来设置,并连接customContextMenuRequested信号来触发右键菜单事件。 对于设置表格的列宽,可以使用setSectionResizeMode方法来调整列的宽度,以获得更好的界面效果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Qt - QTableView](https://blog.csdn.net/weixin_45079970/article/details/127924394)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [QT - QTableView表格视图的列宽设置 - 王严の博客 - CSDN博客1](https://download.csdn.net/download/weixin_35773344/86281472)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值