Add button to UITableViewCell's Accessory View

前端 未结 10 1936
没有蜡笔的小新
没有蜡笔的小新 2021-01-01 16:41

Goal: when a user selects a cell, a button is added to that cell. Within my didSelectRowAtIndexPath function I have the following:

UIButton *downloadButton =         


        
10条回答
  •  耶瑟儿~
    2021-01-01 17:30

    Swift 4 and above: Add button to UITableViewCell's Accessory View

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
        {
            let cell = Table.dequeueReusableCell(withIdentifier: "identifier", for: indexPath)
    
                let accessoryButton = UIButton(type: .custom)
                accessoryButton.addTarget(self, action: #selector(deleteCell(sender:)), for: .touchUpInside)
                accessoryButton.setImage("Add_image", for: .normal) 
                accessoryButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
                accessoryButton.contentMode = .scaleAspectFit
    
                cell.accessoryView = accessoryButton as UIView
    
                return cell
        }
    

    Add Selector Method

        func deleteCell(sender: AnyObject)
        {
            let pointInTable: CGPoint = sender.convert(sender.bounds.origin, to: self.Table)
            let cellIndexPath = self.Table.indexPathForRow(at: pointInTable)
            let point = cellIndexPath!.row
    
        }
    

提交回复
热议问题