Swift开发UITableView常用的一些细节知识点介绍

隐藏分割线、隐藏多余Cell
   //隐藏分割线
   tableView.separatorStyle=UITableViewCellSeparatorStyle.None
   //隐藏多余的cell
   tableView.tableFooterView=UIView(frame:CGRectZero)

分割线头部顶到底、分割线颜色
    //启动、旋转、视图大小位置发生改变、增加子视图等都会调用
    override func viewDidLayoutSubviews ()  {
        tableView.separatorInset = UIEdgeInsetsZero
        tableView.layoutMargins = UIEdgeInsetsZero
        //articleTableView.separatorColor = UIColor.redColor() //分割线颜色
    }
    //没当cell即将出现屏幕时候都会调用此方法
    func tableView (tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        cell.separatorInset = UIEdgeInsetsZero
        cell.layoutMargins = UIEdgeInsetsZero
    }

点击后效果 Cell 背景等更改
  //点击Cell时,没有点击效果
  cell.selectionStyle = UITableViewCellSelectionStyle.None
  //系统默认的颜色  .Blue蓝色-默认 .Grap灰色 .None 无色

 //点击Cell时,自定义选中后的背景视图
 //背景颜色
  cell.selectedBackgroundView = UIView()
  cell.selectedBackgroundView?.backgroundColor = UIColor.clearColor()
  
//背景图片
cell.selectedBackgroundView = UIImageView(image: UIImage(named: article.avatarImage)) //cell 右边的辅助的提示 cell.accessoryType = .DisclosureIndicator

类似button点击效果闪一下
   //在 didSelectRowAtIndexPath 方法内使用
   //点击Cell时 一闪而过 适合转场时候交互 
  tableView.deselectRowAtIndexPath(indexPath, animated: false) // - true 动画慢吞吞,适合不转场时

Tableview视图Cell进入动画 从底部往上弹
    //加载动画 Cell 往上冲 在 viewWillAppear 中使用
    func animateTable(){

        self.tableView.reloadData()

        let cells = tableView.visibleCells
        let tableHeight = tableView.bounds.size.height
        for i in cells {
            let cell: UITableViewCell = i as UITableViewCell
            cell.transform = CGAffineTransformMakeTranslation(0, tableHeight)
        }

        var index = 0  
        for a in cells {
            let cell: UITableViewCell = a as UITableViewCell
            UIView.animateWithDuration(1.0, delay: 0.05 * Double(index), usingSpringWithDamping: 0.8, initialSpringVelocity: 0, 
options: [], animations: {
                cell.transform = CGAffineTransformMakeTranslation(0, 0);
                }, completion: nil)
            index += 1
        }
    }

点击cell展开样式  
//比如一个使用了SB约束好的label ,tag = 666 把他  属性 lines = 0 与 1转换 即显示单行或多行
// -1.记得使用SB设置好约束
override func viewDidLoad() { super.viewDidLoad() // 0.启动自动布局计划 tableView.estimatedRowHeight = 44 tableView.rowHeight = UITableViewAutomaticDimension }
// 1.先声明的一个字典 - 记录每个cell展收状态
var dict:Dictionary Int,Bool = [:]
// 2.根据字典显示cell状态
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) let label = cell.contentView.viewWithTag(666) as! UILabel label.text = "本文导航 \n 1.隐藏分割线\n 2.隐藏多余Cell\n 3.分割线头部顶到底、分割线颜色\n 4.自定义点击后效果 Cell 背景等更改\n 5.类似button点击效果 Cell - 闪一下\n 6.Tableview视图Cell进入动画 从底部往上弹\n 7.TableviewCell使用SB约束 自动布局 \n 8. cell 点击展开" if dict[indexPath.row] == false { label.numberOfLines = 0 } else { label.numberOfLines = 1 } return cell }
// 3. 在 beginUpdates() - endUpdates() 放代码 有连续动画效果
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { tableView.deselectRowAtIndexPath(indexPath, animated: true) //点击闪动效果 let cell = tableView.cellForRowAtIndexPath(indexPath) let label = cell!.contentView.viewWithTag(666 as! UILabel)
        tableView.beginUpdates() //开始
        if label.numberOfLines == 0 {
            label.numberOfLines = 1
            dict[indexPath.row] = true
        } else {
            label.numberOfLines = 0
            dict[indexPath.row] = false
        }
        tableView.endUpdates()
    }

没有数据时候提示 可以自行加入空数据时候显示
//判断有没有数据显示 提示 func showIfNoAnswer() { let imageView = UIImageView(frame: CGRectMake(0, 0, 60, 60)) let image = UIImage(named: "sad") imageView.image = image?.imageWithRenderingMode(.AlwaysTemplate) imageView.tintColor = UIColor.grayColor imageView.center = CGPointMake(self.view.center.x, 145) imageView.tag = 33 // 方便 remove self.view.addSubview(imageView) let label = UILabel(frame: .zero) label.text = "加载失败" label.font = UIFont(name: "New Gulim", size: 20) label.textColor = UIColor.grayColor() label.textAlignment = .Center label.tag = 3 label.sizeToFit() label.backgroundColor = UIColor.clearColor() label.center = CGPointMake(self.view.center.x, 200) view.addSubview(label) } }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hbblzjy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值