美文网首页
UITableView Tips

UITableView Tips

作者: lym不解释 | 来源:发表于2017-02-08 10:49 被阅读58次

UITableView是开发中最常用的UI控件, 可是需求和系统的肯定有出入,UI给的设计肯定有不同的地方,这里就分享一些经常出现的效果和一些常用到的方法,分OC和Swift。

demo常见效果,下拉放大图片、单选、多选、cell下划线位置&颜色、滚动区域、滚动指示器的位置、cell点击动画效果。。。

先来OC

删除单个cell

NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell];

//先移除数组中的cell数据
[self.dataArr removeObject:cell.data];

[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:cellIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];

刷新单个cell section

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

// 刷新第0个section
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];

去掉多余的分割线

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

使用自动布局的话,cell的高度可以自动计算,当然你也可以自己计算或者使用第三方FDTemplateLayoutCell

self.tableView.estimatedRowHeight = 68.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;

避免cell重用的问题

[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

点击cell的时候,有一些操作。比如展开收起等,比起reloadData beginUpdates效果更好

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  [tableView deselectRowAtIndexPath:indexPath animated:true];

  [tableView beginUpdates];
  // 操作
  [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];

  [tableView endUpdates];
}

tableView.visibleCells 这个属性表示屏幕里看到的cell,cell在滚动的时候进行一些展示类的动画

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
  for (UITableViewCell *cell in _tableView.visibleCells) {
      
  }
}

分割线顶头,当然你可以自己画一个,很 简单也很高效,这里使用系统的

/**
*  分割线顶头
*/
-(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];
  }
}

Swift

class TableViewController: UITableViewController {
    
    let str = "囧阿尔法请问哦千万不能佛问\n红企鹅王那儿我新\n奇偶企鹅我娘亲额弄起\n"
    var dict: Dictionary<String , String> = [:]
    
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.tableFooterView = UIView()
        
        tableView.estimatedRowHeight = 100
        tableView.rowHeight = UITableViewAutomaticDimension
    }

    // MARK: - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 30
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        let label = cell.contentView.viewWithTag(100) as! UILabel
        label.text = str
        
        if dict[String(indexPath.row)] == "0" {
            label.numberOfLines = 0
        }else {
            label.numberOfLines = 1
        }
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let cell = tableView.cellForRow(at: indexPath)
        let label = cell?.contentView.viewWithTag(100) as! UILabel
        
        // MARK: cell点击动画处理
        tableView.beginUpdates()
        
        if label.numberOfLines == 0 {
            label.numberOfLines = 1
            dict[String(indexPath.row)] = "1"
        }else {
            label.numberOfLines = 0
            dict[String(indexPath.row)] = "0"
        }
        
        tableView.endUpdates()
    }
    
    // MARK: 处理cell的分割线顶头
    override func viewDidLayoutSubviews() {
        tableView.separatorInset = UIEdgeInsets.zero
        tableView.layoutMargins = UIEdgeInsets.zero
    }
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cell.separatorInset = UIEdgeInsets.zero
        cell.layoutMargins = UIEdgeInsets.zero
    }
    
    // MARK:利用visibleCells属性为cell添加动画
    override func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        for cell in tableView.visibleCells {
            let bootv = cell.contentView.viewWithTag(200)
            let imgv = bootv?.viewWithTag(201)
            let rect = bootv?.convert((bootv?.bounds)!, to: nil)
            
            var Y = UIScreen.main.bounds.size.height - (rect?.origin.y)! - 600
            Y *= 0.2
            if Y > 0 {
                Y = 0
            }
            else if Y < -100 {
                Y = -100
            }
            imgv?.frame.origin.y = Y
        }
    }
}

相关文章

  • UITableView Tips

    1.cell选中时,去掉选中的灰色背景 或者直接让灰色背景不显示 2.取消cell之间的分隔线 默认是UITabl...

  • UITableView Tips

    UITabelView是iOS开发中最常用, 也是最灵活的控件, 所以, 如何打造出更符合用户体验的tableVi...

  • UITableView Tips

    UITableView是开发中最常用的UI控件, 可是需求和系统的肯定有出入,UI给的设计肯定有不同的地方,这里就...

  • UITableView Tips

    1. 延长分割线 2. 不显示没内容的 Cell 3. 修改 Cell 小对勾的颜色 4. 去掉 SectionH...

  • iOS UITableView Header 不悬停的tip

    UITableView Header 不悬停的tips之一 也可以关注我的个人博客 利用contentInset向...

  • UITableView Tips 高级进阶

    1.cell 高度自适应 // 设置 estimatedRowHeight 大于零 self.tableView...

  • js 兼容 hack

    tips tips tips tips tips

  • UITableView的一个小Tips

    去掉某一行Cell的分割线: Cell.separatorInset = UIEdgeInsetsMake(0...

  • 欢迎来到TableView的天地

    swift UITableView UITableView继承于UIScrollView UITableView的...

  • iOS UITableView基础

    一、UITableView的创建 UITableView *tabelView = [[UITableView a...

网友评论

      本文标题:UITableView Tips

      本文链接:https://www.haomeiwen.com/subject/grjvittx.html