【问题标题】:Open a new VC when pressing a button from a tableViewCell按下 tableViewCell 中的按钮时打开一个新的 VC
【发布时间】:2018-02-01 16:46:35
【问题描述】:

问题是当我按下特定 tableViewCell 中的按钮时尝试打开一个新的 VC。 Bellow 是我的 tableView,它正在加载单元格。第 0 部分中的那个是带有按钮的那个。

class NewConversationViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {
        return 1
    } else if section == 1{
        return friendList.count
    }
    return 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NewGroupConversationCell") as! NewGroupConversationTableViewCell

        return cell
    } else if indexPath.section == 1 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NewConversationCell") as! NewConversationTableViewCell
        let friendList = self.friendList[indexPath.row]
        cell.populate(friendList)

        return cell
    }
    return UITableViewCell()
}

下面是带有按钮的单元格和将呈现我想要的特定视图控制器的操作

class NewGroupConversationTableViewCell: UITableViewCell {

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

@IBAction func groupButtonPressed(_ sender: Any) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let messagesViewController = storyboard.instantiateViewController(withIdentifier: "GroupListViewController") as! GroupListViewController
    self.window?.rootViewController?.present(messagesViewController, animated: true, completion: nil)
}

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[VoiceMe.GroupListViewController tableView:numberOfRowsInSection:]:无法识别的选择器发送到实例 0x7fb33171c4d0”

【问题讨论】:

  • 这可能是由于您在连接插座/操作时犯了一个错误。检查它们。
  • 实际上我仔细检查了它们并没有任何线索..
  • 检查您的网点和数据源/代表是否在您的GroupListViewController 上正确设置。这是UITableViewController 吗?错误不在此 VC 的代码中,而是在您推送的代码中。
  • 其实 GroupListViewController 是一个 UIViewController 也包括一个 tableViewController
  • 所以,毕竟我让它工作了,我必须包括委托和数据源,所以你是对的

标签: ios swift uitableview swift3


【解决方案1】:

你不能这样做,所以这些是打开你的 VC 的步骤:

1 - 为您的按钮添加目标函数:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NewGroupConversationCell") as! NewGroupConversationTableViewCell
        cell.youButton.addTarget(self, action:#selector(handleBtnAction(sender:)), for: .touchUpInside)
        cell.youButton.tag = indexPath.row
        return cell
    } 
    ...

2 - 实现你的选择器:

func handleBtnAction(sender: UIButton){
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let messagesViewController = storyboard.instantiateViewController(withIdentifier: "GroupListViewController") as! GroupListViewController
    self.navigationController?.pushViewController(messagesViewController, animated: true)
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 2020-03-08
    • 2020-01-20
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多