当前位置: 移动技术网 > IT编程>移动开发>IOS > Swift开发UITableView常用的一些细节知识点介绍

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

2018年09月22日  | 移动技术网IT编程  | 我要评论

黄道吉日 结婚,万里教务网,雨酷仙境

隐藏分割线、隐藏多余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 //>
 //.checkmark //√    .detaildisclosurebutton // ! >    .detailbutton // !

类似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)
        }
    }

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网