代码之家  ›  专栏  ›  技术社区  ›  Do2

表视图单元格内的按钮

  •  0
  • Do2  · 技术社区  · 9 年前

    在tableview单元格中,我需要两个按钮,一个用于接受,一个用于拒绝。

    在xcode中,我将decline标记设置为2,accept标记设置为3。

    在ViewController内部

      func accept(_ sender: UIButton){
       let buttonTag = sender.tag
        let point = sender.convert(CGPoint.zero, to: friendRequestTableView as UIView)
        let indexPath: IndexPath! = followerReqTableView.indexPathForRow(at: point)
        let user = requestsArray[indexPath.row]
    
      func decline(_ sender: UIButton){
        let buttonTag = sender.tag
        let point = sender.convert(CGPoint.zero, to: friendRequestTableView as UIView)
        let indexPath: IndexPath! = followerReqTableView.indexPathForRow(at: point)
        let user = requestsArray[indexPath.row]
    
    }
    
    
    func tableView(_ tableView:UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        guard let cell = friendRequestTableView.dequeueReusableCell(withIdentifier: "FriendRequestTableViewCell") as? FriendRequestTableViewCell else { return UITableViewCell() }
    
    
        let user = requestsArray[indexPath.row]
    
        let image = cell.viewWithTag(1) as! UIImageView
    
    
    
        cell.confCell(///some user details)
    
        cell.decline.tag = 2
        cell.accept.tag = 3
    
        cell.decline.addTarget(self, action:#selector(self.decline(_:)), for: .touchUpInside)
        cell.decline.addTarget(self, action:#selector(self.accept(_:)), for: .touchUpInside)
    
    
        return cell
    
    }
    

    当我点击decline时,它同时调用两个函数-decline和accept,当我点击accept时,它什么都不做。 我哪里做错了?

    1 回复  |  直到 9 年前
        1
  •  0
  •   Arrabidas92    9 年前

    这很正常,因为在这两行代码的同一个按钮上添加了拒绝和接受操作:

    cell.decline.addTarget(self, action:#selector(self.decline(_:)), for: .touchUpInside)
    cell.decline.addTarget(self, action:#selector(self.accept(_:)), for: .touchUpInside)
    

    只需替换为:

    cell.decline.addTarget(self, action:#selector(self.decline(_:)), for: .touchUpInside)
    cell.accept.addTarget(self, action:#selector(self.accept(_:)), for: .touchUpInside)
    

    将谢绝功能与谢绝按钮相关联,将接受功能与接受按钮相关联。