表视图结构:
表视图:头视图、单元格、尾视图三部分组成
1.表视图的样式:
@property (nonatomic, readonly) UITableViewStyle style;
UITableViewStylePlain(不分组)
UITableViewStyleGrouped(分组的):
2.初始化UITableView:
UITableView *tableView1 = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
3.实现数据源和代理:只有实现数据源才能展现数据
4.NSIndex:
5.常用数据源的方法:
@required
//每一行有多少组(section)数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
//每一行显示什么内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
6.可选方法:
@optional
//这个表示共计多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // Default is 1 if not implemented
单元格的方法
//组中头部视图的标题
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
//组中尾部视图的标题
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
// Editing:可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
// Moving/reordering:可移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
// Index:返回组中的所有标题
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView __TVOS_PROHIBITED;
//返回组中标题的索引
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index __TVOS_PROHIBITED;
//提交编辑的样式
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
//移动到目标单元格
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
7.常用代理方法:
//当单元格被选中时调⽤用
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
// 配置⾏行⾼高
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath;
// 设置section 头部、尾部视图的⾼高度
- (CGFloat)tableView:(UITableView *)tableView
heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView
heightForFooterInSection:(NSInteger)section;
// ⾃自定义section头部、尾部视图,注意:需要指定⾼高度
- (UIView *)tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section;
- (UIView *)tableView:(UITableView *)tableView
viewForFooterInSection:(NSInteger)section;
8.表视图常用属性:
//消除表格的分割线
tableView1.separatorStyle = UITableViewCellSeparatorStyleNone;
//行高
tableView1.rowHeight = 150;
//背景图片
tableView1.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"view-background.jpg"]];
//添加头部视图
tableView1.tableHeaderView =reuseLoopView;
9.代码的实现:
//
// ViewController.m
// tableView
//
// Created by Mac on 15/12/26.
// Copyright © 2015年 Mac. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1.设置tableView的数据源,也就是ViewController自己,它自己也是个OC对象且遵守UITableViewDataSource协议!
self.tableView.dataSource = self;
}
- (BOOL)prefersStatusBarHidden{
return YES;
}
#pragma mark - tableView的数据源方法
//1.设置每一组有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return 2;
}else{
return 3;
}
}
//2.设置总计多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
//3.设置每一行的内容
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//其中,indexPath表示索引
//如:indexPath.row表示某一行,index.section表示某一组
{
UITableViewCell *cell = [[UITableViewCell alloc]init];
// 将第一组第一行的cell的内容设置为红色的“征里”;
if (indexPath.section == 0) {
if (indexPath.row == 0) {
cell.textLabel.text = @"征里";
cell.textLabel.textColor = [UIColor redColor];
return cell;
}else{
cell.textLabel.text = @"征里";
return cell;
}
}else {
cell.textLabel.text = @"征里";
return cell;
}
}
//4.设置每组脚与头显示
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (section == 0) {
return @"第一组的征里!";
}else{
return @"第二组的征里!";
}
}
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
if (section == 0) {
return @"第一组的征里 @@@";
}else{
return @"第二组的征里 @@@";
}
}
@end
运行效果:
154

被折叠的 条评论
为什么被折叠?



