在TableView中展示分层数据
使用tableViewCells的缩进功能:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *result = nil;
static NSString *MyCellIdentifier = @"SimpleCells";
result = [tableView dequeueReusableCellWithIdentifier:MyCellIdentifier];
if(result == nil){
result = [ [UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyCellIdentifier];
}
result.textLabel.text = [NSString stringWithFormat:@"Section %ld,Cell %ld",(long)indexPath.section,(long)indexPath.row];
result.indentationLevel = indexPath.row;
result.indentationWidth = 10.0f;
return result;
}
每个cell有2个相关属性:缩进等级和缩进宽度,缩进等级与缩进宽度简单相乘,所得结果就是偏移量。
启用TableViewCell的滑动删除
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCellEditingStyle result = UITableViewCellEditingStyleNone;
if([tableView isEqual:self.myTableView]){
result = UITableViewCellEditingStyleDelete;
}
return result;
}
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
[myTableView setEditing:editing animated:animated];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if(editingStyle == UITableViewCellEditingStyleDelete){
if(indexPath.row < [self.arrayOfRows count]){
[self.arrayOfRows removeObjectAtIndexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationLeft];
}
}
}
需要UITableViewDelegate代理协议
tableView:editingStyleForRowAtIndexPath:方法能够启动删除功能,它被tableview调用,同时它的返回值决定了tableView允许用户做什么(插入,删除等),它的返回值说明了表中是否该允许同时插入与删除或者同时不。 tableView:commitEditingStyle:forRowAtIndexPath: 方法实现用户的要求删除。后一种方法在委托中定义,但是它的功能有点重载,不只使用这个方法删除数据,也必须要从表中删除行。
deleteRowsAtIndexPaths:withRowAnimation:方法的第二个参数允许你指定一个动画方法,当行从TableView中删除时这个动画方法会被执行。我们的示例说明了当行被删除时它在从右到左的移动过程中消失。
在TableView中构建页眉和页脚
TableView可以有多个页眉页脚,一个TableView的每个Section都可以有它自己的页眉页脚。
#import <UIKit/UIKit.h>
@interface TableView:UIViewController<UITableViewDataSource,UITableViewDelegate>
@property(monatomic,strong)UITableView *myTableView;
@end
@import "TableView.h"
@implementation TableView
@synthesize myTableView;
- (void)viewDidLoad
{
[super
viewDidLoad];
self.title =
@"TableView";
self.view.backgroundColor = [UIColor
grayColor];
myTableView = [[UITableView
alloc]initWithFrame:self.view.bounds
style:UITableViewStyleGrouped];
self.myTableView.dataSource =
self;
self.myTableView.delegate =
self;
self.myTableView.autoresizingMask
= UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view
addSubview:myTableView];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath{
UITableViewCell *result =
nil;
static NSString *CellIdentifier =
@"CellIdentifier";
result = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if(result == nil){
result = [[UITableViewCell
alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
result.textLabel.text = [[NSString
alloc]initWithFormat:@"Cell %ld",(long)indexPath.row];
return result;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 3;
}
/*自定义页眉页脚
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
// UILabel *result = nil;
UIView *result = nil;
if([tableView isEqual:myTableView] && section ==
0){
// result = [[UILabel alloc]initWithFrame:CGRectZero];
// result.text = @" Section 1 Header";
// result.backgroundColor = [UIColor clearColor];
// [result sizeToFit];
UILabel *label = [[UILabel
alloc]initWithFrame:CGRectZero];
label.text =
@"Section 1 Header";
label.backgroundColor = [UIColor
clearColor];
[label
sizeToFit];
label.frame =
CGRectMake(label.frame.origin.x +
10.0f, 5.0f, label.frame.size.width, label.frame.size.height);
CGRect resultFrame =
CGRectMake(0.0f,
0.0f, label.frame.size.height, label.frame.size.width
+ 10.0f);
result = [[UIView
alloc]initWithFrame:resultFrame];
[result
addSubview:label];
}
return result;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UILabel *result =
nil;
if([tableView isEqual:myTableView] && section ==
0){
result = [[UILabel
alloc]initWithFrame:CGRectZero];
result.text =
@" Section 1 Footer";
result.backgroundColor = [UIColor
clearColor];
[result
sizeToFit];
}
return result;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
CGFloat result = 0.0f;
if([tableView isEqual:myTableView] && section ==
0){
result =
30.0f;
}
return result;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
CGFloat result = 0.0f;
if([tableView isEqual:myTableView] && section ==
0){
result =
30.0f;
}
return result;
}
*/
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
CGFloat result = 0.0f;
if([tableView isEqual:myTableView] && section ==
0){
result =
30.0f;
}
return result;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
CGFloat result = 0.0f;
if([tableView isEqual:myTableView] && section ==
0){
result =
30.0f;
}
return result;
}
分享到:
相关推荐
UITableView 删除 Cell Demo Delete
iOS UITableView获取特定位置的cell
[IOS笔记]UITableView及索引条
swift现实UITableView的cell自定义以及删除与添加,添加表的刷新功能
针对UITableView,利用外国友人写的一个优秀的空数据页面显示写的一个分类,方便猿友代码的整理
UItableview cell (自定义)增假,删除 移动
自定义iOS网格组件,基于UITableView 和 UICollectionView 构建的 GridView
当UITableView遇到多cell时,往往我们要建立多个cell,这是就会显得类多,臃肿。 demo采用了工厂模式,用父类cell指针指向子类cell,减少类臃肿,不好维护的效果
UITableViewCell自定义等高
IOS代码中使用自定义UITableView,包含了一些开发过程中需要注意的地方
iOS+折叠文本+UILabel文本折叠+UITableView内UITableViewCell文本折叠
仿IOS式ListVIew UITableView
iOS使用UITableView实现的富文本编辑器iOS使用UITableView实现的富文本编辑器iOS使用UITableView实现的富文本编辑器iOS使用UITableView实现的富文本编辑器iOS使用UITableView实现的富文本编辑器iOS使用UITableView...
长按即可移动cell的自定义UITableView。Github仓库地址:https://github.com/pujiaxin33/JXMovableCellTableView
iOS 开发项目基于 UITableView 的待办事项列表应用
xamarin学习笔记B07(ios的UITableView)是表格控件,用来显示列表数据,它是UIScrollView的子类。类似于Android中的RecyclerView控件。
2.实现了可删除的tableViewCell,向左划动某个cell,cell右侧会出现“删除”按钮,点击即可将当前cell删除,删除伴有简单的动画效果。 3.实现了tableView的下拉刷新功能,下拉tableView的过程中,将依次显示“下拉...
使用场景:UIViewController 上添加添加UITableView,UITableView的cell 时限向左 向右滑动出现多个操作按钮,可以根据具体使用场景修改代码
iOS UITableView查询及索引UISEARCHBAR
使用的是MVC编程框架,KVO数据模型,仅供参考