我正在尝试使用Tableview在Slide菜单中实现Login,Logout.I我在共享首选项中保存登录状态boolean并在创建单元格时更新标题。 当我点击该标签时,它正在打印空字符串。如何可能? 我做错了什么? 希望你理解我的问题。请检查我的代码中的注释。 提前谢谢。
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
}
}
}
}
}
答案 0 :(得分:1)
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let myCell = tableView.cellForRow(at: indexPath) as! MenuTableViewCell
print(myCell.menuTitle.text!)
}