总结 ﹣ UITableView

[color=red]注 : 文章不断更新,转载文章请加上作者[/color]

要实现表格数据形式 , 最常用的做法就是使用UITableView
UITableView继承自UIScrollView, 因此支持垂直滚动,而且性能极佳

[u][b][size=medium]2. UITableView的两种样式(分组与不分组)[/size][/b][/u]


@property (nonatomic, readonly) UITableViewStyle style;


UITableViewStyle是一个枚举
typedef NS_ENUM(NSInteger, UITableViewStyle) {
UITableViewStylePlain, // 平滑样式
UITableViewStyleGrouped // 分组样式
};



[u][b][size=medium]2. UITableView要展示数据,首先要设置数据源[/size][/b][/u]

设置数据源之后,当UITableView需要展示数据的时候,它会[color=red]主动向它的数据源发送消息(调用方法)[/color],根据方法的返回值,它就知道显示多小组,每组多小行,每行什么内容

[color=red]也就是说,只要设置了数据源,并且[u]数据源实现了协议[/u]内的方法,UITableView就会主动展示数据[/color]


//UITableView内部属性,需遵守<UITableViewDataSource>协议
@property (nonatomic, assign) id <UITableViewDataSource> dataSource;


[size=medium][u][b]3. 设置数据源后,实现协议内相应的方法便可展示数据[/b][/u][/size]


[color=red][size=large]以下3个方法,顺序有严格要求,并且numberOfRowsInSection方法,与cellForRowAtIndexPath方法是必须实现的[/size][/color]


//可选方法,设置UITableView中一共有多小个分组, 默认是1(如果不设置就是1)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

//必须实现方法,第section组有多小行
//也就是说,numberOfSectionsInTableView方法返回的数值是多小,这个方法就会调用多小次,每次传入的section值会递增
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

//每一行显示怎样的内容,返回UITableViewCell.
//indexPath是对象,描述的是唯一的一行,它有两个属性,一个是section,一个是row
//也就是说,numberOfSectionsInTableView返回了多小组,numberOfRowsInSection返回是多小行,这个方法就会根据一共有多小组多小行去调用多小次,
//凭indexPath.section组的indexPath.row行 得知每一行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;





//其他一些可选方法:

// 1.
//第section组显示怎样的头部
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;


// 2.
//第section组显示怎样的尾部
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;


// 3.
// 返回值是NSArray 调用这个方法,只要返回一个数组给UITableView ,它就会将数组显示到右面
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
//去数组里面,把数组里面每个title属性拿出来,并且装到数组中返回

return [self.groups valueForKeyPath:@"groupTitle"];
}

// 4.
//全部刷新
[tableView reloadData];


// 5.
//局部刷新
//传入一个数组
[tableView reloadRowsAtIndexPaths:(NSArray *) withRowAnimation:(UITableViewRowAnimation)]


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值