UITableView UITableViewCell NSIndexPath

来源:互联网 发布:cs1.6游戏源码 编辑:程序博客网 时间:2024/06/05 19:56

--------------------------------------------------------------------------NSIndexPath-------------------------------------------------------------------------

1:初始化NSIndexPath (来自: Kid)

inSection 表示TableView 的分组的 第一组数据.

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];  

2:IndexPath 对比是否相同

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. [easyTableView.selectedIndexPath isEqual:indexPath]  

3:通过UITableViewCell 里面的按钮事件 来获取 该按钮所在的IndexPath

1:注册事件:

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. [selectButton addTarget:self action:@selector(FE_selectAnnotation:withEvent:) forControlEvents:UIControlEventTouchUpInside];  
注:@selector 可以获取 UIEvent 对象 

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. NSIndexPath *indexPath = [tableView indexPathForRowAtPoint: [[[event touchesForView: btn] anyObject] locationInView:tableView]];  
  2. NSLog(@"%d",indexPath.row);  



--------------------------------------------------------------------------NSIndexPath--------------------------------------------------------------------------


------------------------------------------------------------------------------------UITableView------------------------------------------------------------------------------------------

1:UITableView reLoadData 重载动态数据时的注意.

因为UITableView的Cell  是以重用的方式去显示数据,所以对Cell中动态数据 都需要设置. 否则可能会乱掉

2:去除选中某行Cell的状态

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. [tableView deselectRowAtIndexPath:indexPath animated:YES];  

3:使用静态UITableView此委托不要实现,否则在运行时将出现警告

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. -(UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     if ([indexPath row] == 0) {  
  4.         return UITableViewCellAccessoryDisclosureIndicator;  
  5.     }else {  
  6.         return UITableViewCellAccessoryNone;  
  7.     }  
  8. }  


4:滑动删除按钮必须同时实现以下两个委托,才能生效

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     return YES;  
  4. }  
  5. -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {   
  6.     NSLog(@"%d",indexPath.row);     
  7. }  

5:在使用UITableView 的

tableHeaderView 时 丢进去的View需要先行设置好 Frame  不然会与后面生成Cell产生重叠的问题

6:UITableView 默认 只支持垂直 可以通过transForm 旋转达到支持水平形式的UITableView

1:网上开源Demo下载地址:

https://github.com/alekseyn/EasyTableView

2:经过我添砖加瓦的版本:

http://download.csdn.net/detail/ysy441088327/4675946


从iOS6以上开始  有了UITableView的亲兄弟 UICollectionView.  它带来更加强大的布局功能.



7:提前获取UITableView 的ContentSize

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. [flowCalendarTableView.tableView layoutIfNeeded];  
  2. NSLog(@"%@",NSStringFromCGSize(flowCalendarTableView.tableView.contentSize));  
8: 去掉UITabelView 默认的 Cell间隔线条
[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;  
9: 下拉刷新实现原理
在UITableView 上面 addSubView 一个 -y 轴的 RefreshView 上去后,那么在拖动TableView的时候 就可以看到刚刚加上去的View
那么此时在ScrollView拖动委托中 进行判断,监听拖拽度足够的情况下触发刷新事件.
在触发事件的同时,修改 contentInset 来保证 RefreshView能够突显出来,顺应其他Cell的拖动显示.
此处记录一点,就是在包含 Section View 的时候 需要加入以下代码,才能保证正常的拖动效果,以不至于出现灵异的拖动显示效果
[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. CGFloat offset = MAX(scrollView.contentOffset.y * -1, 0);  
  2. offset = MIN(offset, 60);  
  3. scrollView.contentInset = UIEdgeInsetsMake(offset, 0.0f, 0.0f, 0.0f);  
注:以上代码通用,无需过多考虑其原理.

10:开启排序拖动编辑功能
[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //开启排序拖动编辑功能  
  2. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath  
  3. {  
  4.     NSLog(@"1");  
  5. }  

11:滑动显示删除按钮时和结束按钮操作时所触发的委托,可以在这两个委托里面控制 Cell内部Frame
[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. - (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     [UIView animateWithDuration:0.25 animations:^{  
  4.         FEUIFriendGroupCell *cell = (FEUIFriendGroupCell *)[tableView cellForRowAtIndexPath:indexPath];  
  5.         cell.friendGroupTitleTextField.Help_width -=60;  
  6.     }];  
  7. }  
  8. - (void)tableView:(UITableView *)tableView  didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath  
  9. {  
  10.     [UIView animateWithDuration:0.25 animations:^{  
  11.         FEUIFriendGroupCell *cell = (FEUIFriendGroupCell *)[tableView cellForRowAtIndexPath:indexPath];  
  12.         cell.friendGroupTitleTextField.Help_width +=60;  
  13.     }];  
  14. }  
注:删除按钮出来时肯定有动画,但是消失时不一定有动画,原因是看你有没有实现 这个委托 accessoryTypeForRowWithIndexPath(搜索一下)

12:让UIPanGestureRecognizer 与 UItableView 的滑动显示 删除按钮  共存

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. [[tableView gestureRecognizers] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {  
  2.     if ([NSStringFromClass([obj class]) isEqualToString:@"UISwipeGestureRecognizer"]) {  
  3.           [self.bookContentContainerView.touchPanGesture requireGestureRecognizerToFail:obj];  
  4.     }  
  5.    
  6. }];  

13:开启UITableView多选编辑功能(iOS 5 or later):
[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. [multiFTPServerListTableView setAllowsMultipleSelectionDuringEditing:YES];  
  2. [multiFTPServerListTableView setEditing:YES];  

在多选状态下,实现单选功能:
[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     NSLog(@"%@",[tableView indexPathsForSelectedRows]);  
  4.     NSArray *indexPathArray = [tableView indexPathsForSelectedRows];  
  5.     [indexPathArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {  
  6.         if ([indexPath isEqual:obj] == NO) {  
  7.             [tableView deselectRowAtIndexPath:obj animated:NO];  
  8.         }  
  9.     }];  
  10. }  

14:让已经选择了多个Cell以后立刻去除所有已选中的效果:
[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. _documentCollectionView.allowsMultipleSelection = NO;  
  2. _documentCollectionView.allowsSelection = NO;  
  3. _documentCollectionView.allowsSelection = YES;  





------------------------------------------------------------------------------------UITableView------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------UITableViewCell---------------------------------------------------------------------------------------


1:设置TableViewCell 的背景颜色

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. UIView *backgrdView =[[UIView alloc] initWithFrame:self.frame];  
  2. backgrdView.backgroundColor=[UIColor colorWithRed:242.0/255 green:242.0/255 blue:242.0/255 alpha:1];  
  3. self.backgroundView= backgrdView;  
  4. [backgrdView release];  
2:控制 自定义UITableViewCell 的 Cell 高度 不要超出显示范围的方式

例如:Cell 高度 有100 但其实只有显示 50. 

第一步是设置好UITableView 没列的高度,通过以下委托

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     return 50;  
  4. }  

第二步 将UITableViewCell 自身设置为 自动遮罩不可见区域

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. [self.layer setMasksToBounds:YES];  

3:为自定义UITableViewCell 设置  reuseIdentifier

在Cell.h 头文件添加

静态取值方法:

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. + (NSString *)reuseIdentifier;  

在Cell.m体文件添加

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //因为是继承关系 所以重写Cell 的 reuseIdentifier 以确定 其值.  
  2. -(NSString *)reuseIdentifier  
  3. {  
  4.    return @"FEUIAddressBookCell";  
  5. }  
  6. //定义一个静态方法来取其值.  
  7. +(NSString *)reuseIdentifier  
  8. {  
  9.    return @"FEUIAddressBookCell";  
  10. }  

UITableView 调用时如下:

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. NSString *SectionsTableIdentifier = [FEUIAddressBookCell reuseIdentifier];  
  2. FEUIAddressBookCell *addressBookCell = (FEUIAddressBookCell *)[tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];  
  3. if(addressBookCell == nil)  
  4. {  
  5.    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FEUIAddressBookCell" owner:self options:nil];  
  6.    addressBookCell = (FEUIAddressBookCell *)[nib objectAtIndex:0];  
  7. }  

通过如上,性能问题已经解决.继续施展拳脚吧!!!


4:UITableView 在Grouped 模式下. 修改Cell 背景颜色的方式

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. cell.backgroundColor = [UIColor whiteColor];  

5:不要在Cell内部的任何控件 使用 setMasksToBounds, 否则拖动动画不流畅


6:设置Cell系统高亮选择样式风格

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. cell.selectionStyle = UITableViewCellSelectionStyleNone;  

注:上面表示取消了.系统默认的高亮选择效果.

---------------------------------------------------------------------------------UITableViewCell---------------------------------------------------------------------------------------


0 0