2016-03-06 33 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" 
       } 
      } 

    } 
相關問題