ios - 登录注销 Tableview Swift

标签 ios swift uitableview swift3

我正在尝试使用 Tableview 在幻灯片菜单中实现登录、注销。我在共享首选项中保存登录状态 bool 值,并在创建单元格时更新标题。 当我单击该标签时,它正在打印空字符串。这怎么可能? 我做错了什么? 希望您理解我的问题。请检查我的代码中的注释。 提前致谢。

let items = [["***"],["***", "***"], ["***", "***", "***"], ["***","***"],["***"],[""]]

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return items[section].count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = menuTableView.dequeueReusableCell(withIdentifier: "menuCellIdentifier") as! MenuTableViewCell        
    if(indexPath.section == 5){
        if(self.preferences.bool(forKey: "Loggedin")){
            cell.menuTitle.text = "Logout"
        }else{
            cell.menuTitle.text = "Login"
        }
    }
    else{
         cell.menuTitle.text=items[indexPath.section][indexPath.row]
    }
    return cell
}


 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    var title_name=items[indexPath.section][indexPath.row]

//Here is my problem
//When there is text(Login/Logout) in menuTitle it is printing empty string
//How this happen????
    print("title_name "+title_name)
//It print empty String
    var myCell = menuTableView.cellForRow(at: indexPath) as! MenuTableViewCell

    if title_name == "Login"
    {        
        print("login clicked")
        //Push to Login view controller and if login is success full update the shared preference
    }

    if title_name == "Logout" {
        print("Logout clicked")          
        Alamofire.request(Common.getLogoutUrl(), method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: NetworkingUtil.getAlamoFireHeader()).responseJSON { (Response) in
            let error = Response.result.error
            let req = Response.request
            let res = Response.response
            let json = Response.data
            SVProgressHUD.dismiss()
            if error != nil {
                if req != nil && res != nil {
                    print(req!)
                    print(res!)
                }

            }else{

                var parsedjson : JSON!
                do {
                    parsedjson = try JSON(data : json!)
                    print(parsedjson)
                } catch {
                    print(error)
                    // or display a dialog
                }        
              else{                                              
                    self.preferences.set(false, forKey: "Loggedin") 
                    myCell.menuTitle.text = "Login"
                    self.menuTableView.reloadData()
                    //Push to home view controller            
                }
            }
        }   
    }
}

最佳答案

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let myCell = tableView.cellForRow(at: indexPath) as! MenuTableViewCell
    print(myCell.menuTitle.text!)
}

关于ios - 登录注销 Tableview Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49345196/

相关文章:

ios - 当 ForEach 中发生 onDelete 时如何更新其他 CoreData?

ios - OpenCV 错误 : Assertion failed on iOS

ios - 如何防止内容滚动到 UITableView 页脚后面

ios - NSTemporaryDirectory 中的 Alamofire MultiPartForm 文件

iOS更改单元格边框色调颜色

ios - AutoLayout 根据 child 设置 UITableViewCell 的高度

ios - 震动不会停止

ios - 如何提供TestFlight iOS beta应用程序的反馈?

objective-c - 在 Objective C 类文件中调用 swift 完成 block 。错误: parameter name omitted

ios - 动态表格 View 高度