iOS-------UITableView
来源:互联网 发布:python中迭代器 编辑:程序博客网 时间:2024/06/07 19:17
UITableView是iOS中显示多条数据的控件,UITableView的功能很强大,做笔记记录一下
plain—默认的格式,一般的tableview
group—这种风格的tableview,如果tableview的section有headview的话,tableview的section header会随着tableview一起滚动,代码演示
self.tableview.stylt=UITableViewStyleGrouped;
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 220;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return self.headerView;
}
return nil;
}
(2) estimatedRowHeight:在iOS 8之前,如果想在表视图中展示可变高度的动态内容时,你需要手动计算行高,而Self Sizing Cells为展示动态内容提供了一个解决方案。以下是你使用Self Sizing Cells时需要注意的事项:
1.cell用Auto Layout约束
2.指定表视图的estimatedRowHeight
3.将表视图的rowHeight属性设置为UITableViewAutomaticDimension
代码为
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
参考链接为http://www.cocoachina.com/ios/20140922/9717.html
(3)estimatedSectionHeaderHeight,estimatedSectionFooterHeight同理
(4)separatorInset--自定义分割线的inset
(5)backgroudview--在tableview中添加背景控件,这个控件不管宽高设定为多少,都默认为tableview的宽高
(6)返回表格中指定indexPath对应的cell
- ( UITableViewCell *)cellForRowAtIndexPath:( NSIndexPath *)indexPath;
(7)返回指定cell的indexPath
- ( NSIndexPath *)indexPathForCell:( UITableViewCell *)cell;
(8)返回表格中指定点所在的indexPath
- ( NSIndexPath *)indexPathForRowAtPoint:( CGPoint )point;
(9)返回表格中指定区域内所有indexPath 组成的数组
- ( NSArray *)indexPathsForRowsInRect:( CGRect )rect;
(10)返回表格中所有可见区域内cell的数组
- ( NSArray *)visibleCells;
(11)返回表格中所有可见区域内cell对应indexPath所组成的数组
- ( NSArray *)indexPathsForVisibleRows;
(12)控制该表格滚动到指定indexPath对应的cell的顶端 中间 或者下方
- ( void )scrollToRowAtIndexPath:( NSIndexPath *)indexPath atScrollPosition:( UITableViewScrollPosition )scrollPosition animated:( BOOL )animated;
(13)控制该表格滚动到选中cell的顶端 中间 或者下方
-( void )scrollToNearestSelectedRowAtScrollPosition:( UITableViewScrollPosition )scrollPosition animated:( BOOL )animated;
(14)调用- (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;方法时,应该先修改- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section相应的数值
tableview实用小技巧---参考http://ios.jobbole.com/84377/
a:当我们的数据未能显示满一屏幕的时候,UITableView会显示多余的横线,这个时候你如果希望去掉这些横线,你可以加上这句话
b:UITableView的分割线默认是开头空15像素点的(好像是15来着~~),产品经理有时候希望能够定格显示,那么你可能会这么做。
但是你很快就会发现这么做并没有效果,这是因为
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;--->
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section——>
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath——>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
如果想对tableview添加、删除、移动数据,则这样:
(1)仅删除:
if(editingStyle==UITableViewCellEditingStyleDelete){
[self.array removeObjectAtIndex:indexPath.row];
[self.tablevi deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
(2)仅添加
属性:
(1)style:tableview的风格,分为两种:plain—默认的格式,一般的tableview
group—这种风格的tableview,如果tableview的section有headview的话,tableview的section header会随着tableview一起滚动,代码演示
self.tableview.stylt=UITableViewStyleGrouped;
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 220;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return self.headerView;
}
return nil;
}
(2) estimatedRowHeight:在iOS 8之前,如果想在表视图中展示可变高度的动态内容时,你需要手动计算行高,而Self Sizing Cells为展示动态内容提供了一个解决方案。以下是你使用Self Sizing Cells时需要注意的事项:
1.cell用Auto Layout约束
2.指定表视图的estimatedRowHeight
3.将表视图的rowHeight属性设置为UITableViewAutomaticDimension
代码为
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
参考链接为http://www.cocoachina.com/ios/20140922/9717.html
(3)estimatedSectionHeaderHeight,estimatedSectionFooterHeight同理
(4)separatorInset--自定义分割线的inset
(5)backgroudview--在tableview中添加背景控件,这个控件不管宽高设定为多少,都默认为tableview的宽高
(6)返回表格中指定indexPath对应的cell
- ( UITableViewCell *)cellForRowAtIndexPath:( NSIndexPath *)indexPath;
(7)返回指定cell的indexPath
- ( NSIndexPath *)indexPathForCell:( UITableViewCell *)cell;
(8)返回表格中指定点所在的indexPath
- ( NSIndexPath *)indexPathForRowAtPoint:( CGPoint )point;
(9)返回表格中指定区域内所有indexPath 组成的数组
- ( NSArray *)indexPathsForRowsInRect:( CGRect )rect;
(10)返回表格中所有可见区域内cell的数组
- ( NSArray *)visibleCells;
(11)返回表格中所有可见区域内cell对应indexPath所组成的数组
- ( NSArray *)indexPathsForVisibleRows;
(12)控制该表格滚动到指定indexPath对应的cell的顶端 中间 或者下方
- ( void )scrollToRowAtIndexPath:( NSIndexPath *)indexPath atScrollPosition:( UITableViewScrollPosition )scrollPosition animated:( BOOL )animated;
(13)控制该表格滚动到选中cell的顶端 中间 或者下方
-( void )scrollToNearestSelectedRowAtScrollPosition:( UITableViewScrollPosition )scrollPosition animated:( BOOL )animated;
(14)调用- (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;方法时,应该先修改- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section相应的数值
tableViewCell 属性
shouldIndentWhileEditing--当编辑时可以缩进
indentationWidth--缩进宽度
indentationLevel--缩进级别,使cell内容向右缩进
cell.indentationWidth=0; cell.indentationLevel=0;
cell.indentationWidth=2; cell.indentationLevel=10;
tableview实用小技巧---参考http://ios.jobbole.com/84377/
a:当我们的数据未能显示满一屏幕的时候,UITableView会显示多余的横线,这个时候你如果希望去掉这些横线,你可以加上这句话
self.tableView.tableFooterView = [[UIView alloc]init];
b:UITableView的分割线默认是开头空15像素点的(好像是15来着~~),产品经理有时候希望能够定格显示,那么你可能会这么做。
self.tableView.separatorInset = UIEdgeInsetsZero;
但是你很快就会发现这么做并没有效果,这是因为
separatorInset
这个属性在iOS7以后就已经失效了,但是我们还是能够达到同样的效果,你可以在你的tablevView的代理协议实现界面加上下面这段代码:-(void)viewDidLayoutSubviews{ if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) { [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)]; } if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) { [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)]; }}-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsZero]; } if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:UIEdgeInsetsZero]; }}
tableview在显示数据时,先执行
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;--->
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section——>
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath——>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
如果想对tableview添加、删除、移动数据,则这样:
(1)仅删除:
a:左滑删除
if(editingStyle==UITableViewCellEditingStyleDelete){
[self.array removeObjectAtIndex:indexPath.row];
[self.tablevi deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
b:编辑删除
<pre name="code" class="objc">- (IBAction)editClick:(id)sender { self.tablevi.editing?[self.tablevi setEditing:NO animated:YES]:[self.tablevi setEditing:YES animated:YES]; }//编辑按钮点击事件- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewCellEditingStyleDelete;}- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if(editingStyle==UITableViewCellEditingStyleDelete){ [self.array removeObjectAtIndex:indexPath.row]; [self.tablevi deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; }}
(2)仅添加
- (IBAction)editClick:(id)sender { self.tablevi.editing?[self.tablevi setEditing:NO animated:YES]:[self.tablevi setEditing:YES animated:YES]; }//编辑按钮点击事件- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewCellEditingStyleInsert;}- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if(editingStyle==UITableViewCellEditingStyleInsert){ [self.array insertObject:@"F" atIndex:indexPath.row+1]; NSIndexPath *indexPath1=[NSIndexPath indexPathForItem:indexPath.row+1 inSection:0]; [self.tablevi insertRowsAtIndexPaths:@[indexPath1] withRowAnimation:UITableViewRowAnimationLeft]; }}(3)移动行
- (IBAction)editClick:(id)sender { self.tablevi.editing?[self.tablevi setEditing:NO animated:YES]:[self.tablevi setEditing:YES animated:YES]; }//编辑按钮点击事件- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewCellEditingStyleNone;}- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{ return YES;}- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ [self.array exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];}(4)删除和添加
//编辑按钮点击事件- (IBAction)editClick:(id)sender { self.tablevi.editing?[self.tablevi setEditing:NO animated:YES]:[self.tablevi setEditing:YES animated:YES]; self.tablevi.allowsMultipleSelectionDuringEditing=NO; self.tablevi.allowsMultipleSelection=NO; }//确定按钮点击事件 - (IBAction)sureClick:(id)sender { NSArray <NSIndexPath *> *indexPaths=[self.tablevi indexPathsForSelectedRows]; for(NSIndexPath *index in indexPaths){ [self.array removeObjectAtIndex:index.row]; } [self.tablevi beginUpdates]; [self.tablevi deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];//需要执行的操作 [self.tablevi endUpdates]; }- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;}
1 0
- IOS UITableview
- ios UITableView
- iOS- UITableView
- ios UITableView
- IOS UITableView
- IOS UITableView
- Ios-UITableView
- iOS UITableView
- iOS UITableView
- IOS-UITableView
- IOS UITableView
- iOS-------UITableView
- iOS UITableView
- iOS UITableView
- ios UITableView分页加载
- IOS UITableView下拉刷新
- IOS控件UITableView详解
- ios UITableView分页加载
- UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)
- Java单链表基本操作(七)--排序
- [LeetCode 326] Power of Three
- .net 单元测试
- java多线程的同步和死锁
- iOS-------UITableView
- 彻底理解ThreadLocal
- java设计模式之创建型模式-工厂方法模式
- 社会化分享 使用as
- Activity的启动流程
- 【转载】循环结构中break、continue、return和exit的区别
- HashSet和HashMap分析
- Android Studio打包Android Library为jar方案
- js循环绑定事件解决方案