第四记- Swift版百思不得

继续上次没完成的模块进行讲解,首先先下效果图


这个效果其实实现起来很简单,但是用Swift写的话会有一个坑,因为Swift是类型安全性极强的语言,当值为nil时就蹦了,

分析:该效果是控制器中包含两个TableView,点击第一个TableView中的某个cell时,右边对应的TableView就刷新数据,大致就是这么一个情况

1:两个TableView都实现协议方法,用一个数组存储左边的数据(都是模型),用一个模型变量表示左边选择的某个cell数据,

  /**  当前选中左边的标签数据  */
     var leftModel:XYFriendModel?
    
    /**  左侧标签数据  */
    var dataArr =  NSMutableArray()

2:请求数据,在代理方法中判断是左边的 TableView还是右边的,然后设置对应的数据

extension XYMyFriendController :UITableViewDelegate,UITableViewDataSource{
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        
        if tableView == leftView{
            
            return dataArr.count
        }else{
//            ?? -- 空合运算符 为空时  取0 否则取本值
            return leftModel?.users.count ?? 0
        }
    }
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        if (tableView == leftView){
            
            let FriendModel = self.dataArr[indexPath.row] as? XYFriendModel

            let myfriend =  tableView.dequeueReusableCellWithIdentifier(MYFriendIdentifier) as! XYMyFriendTableCell
            myfriend.FriendModel = FriendModel
            return myfriend
        }else{
 
             let myfrienduser =  tableView.dequeueReusableCellWithIdentifier(MYFriendUserIdentifier) as! XYTableUserCell
            myfrienduser.UserModel = leftModel?.users[indexPath.row] as? XYUserModel
            return myfrienduser
        }
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
        
        if (tableView == self.leftView){
            
            //将当前选中的值赋值给 leftModel
           leftModel = dataArr[indexPath.row] as? XYFriendModel
            self.rightView.reloadData()
            
            //同时刷新右边表哥
            self.rightView.mj_header.beginRefreshing()
            // 判断是否有过用户数据
            // 防止重新加载数据
            if (self.leftModel?.users.count == 0){
                rightView.mj_header.beginRefreshing()
            }
        }
    }
}

3:在加载左边数据的方法中,自动选中第一行

 //先走一遍选中方法,获取到当前选中的标签,这样就不会出现nil值崩溃现象
            weakSelf?.tableView((weakSelf?.leftView)!, didSelectRowAtIndexPath:NSIndexPath.init(forRow: 0, inSection: 0))
            //默认第一行
            weakSelf?.leftView.selectRowAtIndexPath(NSIndexPath.init(forRow: 0, inSection: 0), animated: false, scrollPosition: .Top)
            

4:左边cell选中变为红色,其实实现起来很简单,无非就是记录选中状态,改变其cell

就是重写cell的setSelected方法

 /**
     * 这个方法可以用来监听cell的选中和取消选中
     */
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        XYDEBUG(selected)
        //选择为红色文字
        self.textLabel?.textColor = selected ? UIColor.redColor(): UIColor.darkGrayColor()
        selectIndicator.hidden = !selected
        
    }


到此为止,就能实现效果图的功能了。




这个效果我是用的xib实现的,当点击注册账号时,实现一个动画跳转到注册界面,无非就是改变了布局中登录界面左边距,重点就是说一个点击手机号时,placeholder颜色变红,这里用到时runtime机制,我是利用runtime机制获取到了UITextField的所有属性,然后利用KVC改变值

  setValue(UIColor.redColor(), forKeyPath:"_placeholderLabel.textColor")

接下来就剩下最后一个我的模块了,写完最后一个我就会发布一个完成版项目,希望一起学习和借鉴




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值