【问题标题】:click on TableViewCell to open profile view of current user单击 TableViewCell 以打开当前用户的配置文件视图
【发布时间】:2023-04-01 21:42:01
【问题描述】:

我正在使用 firebase 数据库,并在 index path.row 有一个表格视图,显示用户的姓名、年龄、位置和图像。当单击单元格以显示包含更多信息和用户照片的用户个人资料时,我将如何显示另一个视图。这是我的 tableView 的代码。

 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return userList.count

}

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "prefCell", for: indexPath) as! SFPTableViewCell

      let user = userList[indexPath.row]

    cell.name.text = userList[indexPath.row].name
    cell.location.text = userList[indexPath.row].location


    if let imageURL = user.image {

        cell.imageOne.loadImageUsingCacheWithUrlString(urlString: imageURL)
    }       
          return cell
}

    }

【问题讨论】:

  • 添加表格视图委托“didSelectRowAtIndexPath”

标签: swift uitableview firebase firebase-realtime-database tableview


【解决方案1】:

使用代码使用 didSelectRowAtIndexPath。

使用情节提要:

第 1 步:选择表格视图单元格。

第 2 步:Control + 单击鼠标并在目标视图中拖动。

第 3 步:给出任何一个 segue。

第 4 步:完成。

【讨论】:

    【解决方案2】:

    在下面添加这个代码

    override  func tableView(_ tableView: UITableView, didSelectRowAtindexPath: IndexPath){
    
       let detailVC = storyboard.instantiateViewControllerWithIdentifier("MyDetailVC") as MyDetailVC // get the obect of other VC
       detailVC.myImage = <your_image> // assign your image which you wanted to send to other VC.
       self.navigationController?.pushViewController(detailVC,animated: true // Push to other VC
    }
    

    MyDetailVC.swift

    var myImage : UIImage? // Create variable of image type in required VC where you wanted to send data
    

    现在在MyDetailVC.swift 中使用您的图片。

    您可以提出任何进一步的问题。

    【讨论】:

    • 我可以通过电子邮件向您发送我对此的疑问
    【解决方案3】:

    刚刚使用UITableViewDelegate 方法"didSelectRowAtIndexPath" 并将图像及其indexPath发送到其他视图控制器

     override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let sb = UIStoryboard(name: "Main", bundle: nil)
            let detailVC = sb.instantiateViewController(withIdentifier: "ImageVC") as! DetailViewController
            detailVC.strImage  = self.arrImage[indexPath.row]
            self.navigationController?.pushViewController(detailVC, animated: true)
        }
    

    DetailViewController 中取一个变量

    var strImage : String = ""
    

    并将这个变量赋值给 UIImagView

     imgView.image = UIImage(named: strImage) 
    

    【讨论】:

    • 您是从 WebService 获取图像吗?
    猜你喜欢
    • 2013-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 2020-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多