iOS tableView的偏门问题

来源:互联网 发布:java聊天室开源代码 编辑:程序博客网 时间:2024/05/20 14:23
 //1.设置某个单元格不可选中

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath*)indexPath{

    

    if (indexPath.row == 0) {

        

        return nil;

    }

    return indexPath;

}



//2.设置组的分割线为nil     设置tableview 背景颜色和分割线的颜色一样即可

http://mp.weixin.qq.com/s?zheng__biz=MzAxMzE2Mjc2Ng==&mid=403515369&idx=1&sn=5bbc00138c5580bb601de93ffbb7d682&scene=23&srcid=04110iLp6PwOH1ayYu6nrb63#rd
//3.设置tableview的单选
        NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:_index inSection:1];

        cell = [self cellForRowAtIndexPath:lastIndex];

       // cell.accessoryType = UITableViewCellAccessoryNone;

        cell.selectedAction.hidden = YES;

        // 选中操作

        cell = [self  cellForRowAtIndexPath:indexPath];

       // cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

        cell.selectedAction.hidden = NO;

        // 保存选中的

        _index = indexPath.row;

 
//4.循环遍历放入单元格内容       

else if (indexPath.section == 1){

        

         TwoCell *twoCell = [tableView dequeueReusableCellWithIdentifier:identifyTwoforIndexPath:indexPath];

        

        NSArray *array = @[@"请选择充值金额",@"10地球币",@"20地球币",@"50地球币",@"100地球币",@"150地球币",@"200地球币",@"888地球币",@"1024地球币"];

        

        twoCell.twoLabel.text = [array objectAtIndex:indexPath.row];

       

        

        return twoCell;


//5.设置箭头

  cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    


UITableView是工程开发中最经常使用到的UI控件,但是你真的了解它嘛,这里记录几点有用的但你可能并不知道的。


  • 当我们的数据未能显示满一屏幕的时候,UITableView会显示多余的横线,这个时候你如果希望去掉这些横线,你可以加上这句话。


self.tableView.tableFooterView = [[UIViewalloc]init];


  • UITableView的分割线默认是开头空15像素点的(好像是15来着~~),产品经理有时候希望能够定格显示,那么你可能会这么做。


   self.tableView.separatorInset = UIEdgeInsetsZero;


但是你很快就会发现这么做并没有效果,这是因为separatorInset这个属性在iOS7以后就已经失效了,但是我们还是能够达到同样的效果,你可以在你的tablevView的代理协议实现界面加上下面这段代码:


/**

*  分割线顶头

*/

-(void)viewDidLayoutSubviews

{

  if([self.tableViewrespondsToSelector:@selector(setSeparatorInset:)]){

      [self.tableViewsetSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];

  }

 

  if([self.tableViewrespondsToSelector:@selector(setLayoutMargins:)]){

      [self.tableViewsetLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];

  }

}

-(void)tableView:(UITableView *)tableViewwillDisplayCell:(UITableViewCell *)cellforRowAtIndexPath:(NSIndexPath *)indexPath

{

  if([cellrespondsToSelector:@selector(setSeparatorInset:)]){

      [cellsetSeparatorInset:UIEdgeInsetsZero];

  }

  if([cellrespondsToSelector:@selector(setLayoutMargins:)]){

      [cellsetLayoutMargins:UIEdgeInsetsZero];

  }

}


再次运行,好了我们的UITableView终于顶头显示分割线了。


很多情况下我们的UITableViewCell的高度是动态不确定的,比如说很多聊天的界面都需要我们去动态的计算cell的高度,你可能会在heightForRowAtIndexPath代理协议方法中返回你计算好的cell高度,然后在苹果推出约束以后,我们其实有更加方便的方法去实现相同的效果。你可以尝试在你的代码中加入以下两行代码:


self.tableView.estimatedRowHeight = 68.0;

self.tableView.rowHeight = UITableViewAutomaticDimension;


再次运行你的程序,其实你发现了好像你的cell并没有动态的返回高度,这是因为上面说了,这两行代码必须配合约束来使用。


我们拖出一个SB,然后在cell上放上一个label,讲label的numberOfLines属性设置为0,然后设置好label的上下左右约束,然后再对label的内容进行赋值,再次运行你的程序,这个时候你的cell就会动态的显示高度了,label的高度取决于你的内容的多少,同时按照你的约束进行显示。


-你可能写过这样下面这样的代码


-(void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath{

  [tableViewdeselectRowAtIndexPath:indexPathanimated:true];

  [tableViewbeginUpdates];

  ROW--;//此操作表示减少数据源的个数。

  [tableViewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationRight];

  [tableViewendUpdates];

}


用一个动画来删除某一个cell,其中有两行代码特别有意思:


[tableViewbeginUpdates];

[tableViewendUpdates];


这俩吧其实和[tableView reloadData]作用类似,但是这俩货却能非常轻松的创造出不错的效果,比如说和我们上一点说的用约束来控制label的行高相结合的是的时候,我们先来看一下效果:


一个tableView点击缩放的效果

其实我的代码很少,核心代码只有以下几行:


-(void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableViewdeselectRowAtIndexPath:indexPathanimated:true];

    UITableViewCell *cell = [tableViewcellForRowAtIndexPath:indexPath];

    UILabel *label = [cell.contentViewviewWithTag:1000];

    [tableViewbeginUpdates];

    if(label.numberOfLines == 0){

        label.numberOfLines = 1;

    }else{

        label.numberOfLines = 0;

    }

    [tableViewendUpdates];

}


我用SB创建了一个UITableView,然后在cell上放置了一个label,初始化label 的numberOfLines然后在界面上设置tableView


self.tableView.estimatedRowHeight = 68.0;

self.tableView.rowHeight = UITableViewAutomaticDimension;


然后在他的点击动作中改变label的numberOfLines,同时结合使用:


[tableViewbeginUpdates];

[tableViewendUpdates];


像上面po出来的代码那样,这个时候你如果使用[tableView reloadData]也能够达到改变cell高度的效果,但是界面上就不会有使用[tableView beginUpdates]那么流畅,以此类推,其实在很多地方都可以用[tableView beginUpdates]来代替[tableView reloadData]来达到更好的效果.


你可能会经常忽略UITableView的一些属性和回调,必须下面这个方法:


-(void)tableView:(UITableView *)tableViewwillDisplayCell:(UITableViewCell *)cellforRowAtIndexPath:(NSIndexPath *)indexPath{

  CGFloatoffSet = tableView.contentOffset.y;

  if(offSet<=0){

      return;

  }

  CGRectoldRect = cell.frame;

  CGRectnewRect = cell.frame;

  newRect.origin.x += 50;

  cell.frame = newRect;

  [UIViewanimateWithDuration:0.5animations:^{

      cell.frame = oldRect;

  }];

}


如果你这么写会简单的有一个展示的动画,这个回调就是在cell展示到屏幕的时候发起的动作。

还有这个属性:tableView.visibleCells,你的产品经理可能会要求你的cell在滚动的时候进行一些展示类的动画—-滚动的时候进行展开收起之类的,这样的话你可以这么做:


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

  for(UITableViewCell *cell in_tableView.visibleCells){

      /**

       *  你可以在这里对当前的cell进行一些操作

       *

       */

  }

}


这个属性会返回即将展示到屏幕上的cell,而放在这个滚动的回掉中你就可以对你的cell进行不停的调整了,具体能做出什么动画,就靠你的想象能力了。


tableView可能会造成你的Controller过于庞大,或许你可以使用MVVM类似的构架来瘦身你的Controller。。。。。。





0 0
原创粉丝点击