开发笔记-tableView展示数据常见设置

mac2022-06-30  21

如何让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的常见设置

// 设置每一行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;// 设置选中的背景viewUIView *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

最新回复(0)