【问题标题】:Handle CellForRowAtIndexPath in SWRevealViewController in swift?快速处理 SWRevealViewController 中的 CellForRowAtIndexPath?
【发布时间】:2016-03-06 07:57:21
【问题描述】:

问题: 如果用户“登录”我想在单元格中显示“注销”选项,反之亦然。但我 在 Storyboard 中创建 UI

我已经创建了自定义 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!

}

但我得到了零单元格。我设置了数据源、委托、表连接。如何解决?

【问题讨论】:

  • 能否显示错误报告
  • 只是“在展开可选时意外发现 nil”
  • 让单元格:UITableViewCell? = menuListView.cellForRowAtIndexPath(indexPath)
  • 我无法从 interfacebilder 获取单元格,因为我没有将自定义类设置为单元格,因为它在 SWRevealViewcontroller 中不需要。所以得到零单元格。

标签: ios swift swrevealviewcontroller


【解决方案1】:

使用以下代码修复。使用 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"
                }
            }

    }

【讨论】:

    猜你喜欢
    • 2015-05-21
    • 1970-01-01
    • 2014-02-19
    • 2019-04-08
    • 1970-01-01
    • 2012-09-21
    • 2018-03-04
    • 2021-03-19
    相关资源
    最近更新 更多