首页 > 技术文章 > UITableView 总结(一)

Mantis-man 2016-01-05 10:08 原文

一、UITableView 有两种设置风格

1 typedef NS_ENUM(NSInteger, UITableViewStyle) {
2     UITableViewStylePlain,          // regular table view
3     UITableViewStyleGrouped         // preferences style table view
4 };

1、设置为 UITableViewStylePlain,下底有多余的线。如下图:

       

 

  是不是觉得好恶心,如何去掉?很简单,代码如下:

1 self.tableView.tableFooterView = [[UIView alloc]init];

 

2、设置为 UITableViewStylePlain 多组,滑动时,区头挡着不随cell滑动上去,确实不想要这种效果要如何觉得呢。代码如下:

#pragma mark - scrollView delegate
//delete UItableview headerview黏性
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.tableView){
     CGFloat sectionHeaderHeight
= 10; if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) { scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0); } else if (scrollView.contentOffset.y>=sectionHeaderHeight) { scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0); } }
}

 

3、设置为 UITableViewStyleGrouped ,头部有默认间距,如何设置自己需要高度。代码如下:

          

self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 0.001f)];

 

二、系统不一,出现的问题。

1、由于iOS8及以上每行cell线条左边不靠边,而iOS7是左边是靠边。若有需求要iOS8以上系统的cell线条左边靠边。代码如下:

    

 1 // 解决ios8及以上 分割线有边緣问题
 2 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
 3 {
 4     if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
 5         [cell setSeparatorInset:UIEdgeInsetsZero];
 6     }
 7     
 8     if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
 9         [cell setLayoutMargins:UIEdgeInsetsZero];
10     }
11 }
12 
13 // 解决ios8及以上 分割线有边緣问题
14 -(void)viewDidLayoutSubviews
15 {
16     if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
17         [self.tableView setSeparatorInset:UIEdgeInsetsZero];
18     }
19     
20     if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
21         [self.tableView setLayoutMargins:UIEdgeInsetsZero];
22     }
23 }
tableView line

 

三、刷新

1、一个section刷新   
NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];   
[tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];   

2、一个cell刷新   
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:3 inSection:0];    
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone]; 

 

四、获取点击cell的坐标点

 CGRect  popoverRect = [tableView convertRect:[tableView rectForRowAtIndexPath:indexPath] toView:[tableView superview]]; 

推荐阅读