2016-03-06 53 views
0

问题: 如果用户“登录”,我想在单元格中显示“注销”选项,反之亦然。但我在故事板创建的UI 在Swift中处理SWRevealViewController中的CellForRowAtIndexPath?

enter image description here

我已创建自定义的UITableView类的cellForRowAtIndexPath看起来像

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    // var cell : UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell! 

    let cell : UITableViewCell? = menuListView.cellForRowAtIndexPath(indexPath) 

    if indexPath.row == 20 
    { 
     let titleLabel = cell?.contentView.viewWithTag(100) as! UILabel 
     if(NSUserDefaults.standardUserDefaults().integerForKey("UserID") <= 0){ 
      //logout 
       cell?.contentView.backgroundColor = UIColor(red: 6.0/255, green: 46.0/255, blue: 107.0/255, alpha: 1.0) 
       titleLabel.text = "Login" 
     }else{ 
       //user is login 
       cell?.contentView.backgroundColor = UIColor.redColor() 
       titleLabel.text = "Logout" 
     } 
    } 

    return cell! 

} 

但我正在逐渐零细胞。我设置数据源,委托,表连接。如何解决?

+0

可以显示错误reprt –

+0

只是“”意外发现零而展开可选”,其中 –

+0

线兄弟 –

回答

0

通过使用以下代码进行修复。使用tableviews委托方法willDisplayCell。

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 

      if indexPath.row == 20 
      { 
       let titleLabel = cell.contentView.viewWithTag(100) as! UILabel 
       if(NSUserDefaults.standardUserDefaults().integerForKey("UserID") <= 0){ 
        //logout 
        cell.contentView.backgroundColor = UIColor(red: 6.0/255, green: 46.0/255, blue: 107.0/255, alpha: 1.0) 
        titleLabel.text = "Login" 
       }else{ 
        //user is login 
        cell.contentView.backgroundColor = UIColor.redColor() 
        titleLabel.text = "Logout" 
       } 
      } 

    } 
相关问题