0

Problem: IF User "Login" the i want show "Logout" option in cell and vice versa. but i created UI in Storyboard as

enter image description here

I have created custom UITableView class and cellForRowAtIndexPath look like

 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!

}

but i am getting nil cell. i set Datasource,Delegate,table connection.How to fix that?

Avijit Nagare
  • 8,482
  • 7
  • 39
  • 68

1 Answers1

0

Fixed by using following code. use tableviews delegate method 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"
                }
            }

    }
Avijit Nagare
  • 8,482
  • 7
  • 39
  • 68