Open URL with a button inside a table view cell

前端 未结 3 1600
轻奢々
轻奢々 2021-01-22 11:58

I want to include a button in each table cell that opens a URL.

I\'ve created tables (using an array) with images and labels just fine, however I\'m confused how to crea

相关标签:
3条回答
  • 2021-01-22 12:17

    Just create UIButton object in viewDidLoad and add this button as a sub view on cell in cellForRowAtIndexPath function. Take Burton's frame as per your requirement.

    0 讨论(0)
  • 2021-01-22 12:24

    Swift 4

    This is best way to get indexPath using touchPoint

    class YourTableViewController: UITableViewController {
    
        // ...
    
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "SwiftyCell", for: indexPath) as! SwiftyTableViewCell
    
            cell.label.text = "This is cell number \(indexPath.row)"
    
            // WRONG! When cells get reused, these actions will get added again! That's not what we want.
            // Of course, we could get around this by jumping through some hoops, but maybe there's a better solution...
            cell.yourButton.addTarget(self, action: #selector(self.yourButtonTapped(_:)), for: .touchUpInside)
    
            return cell
        }
    
        func yourButtonTapped(_ sender: Any?) {
            let point = tableView.convert(sender.center, from: sender.superview!)
    
            if let wantedIndexPath = tableView.indexPathForItem(at: point) {
                let cell = tableView.cellForItem(at: wantedIndexPath) as! SwiftyCell
    
            }
        }
        // ...
    
    }
    

    For more details you can follow this tutorials

    0 讨论(0)
  • 2021-01-22 12:26

    Here's how I usually solve this. Create a delegate for your UITableViewCell subclass, and set the view controller owning the tableView as its delegate. Add methods for the interactions that happens inside the cell.

    protocol YourTableViewCellDelegate: class {
        func customCellDidPressUrlButton(_ yourTableCell: YourTableViewCell)
    }
    
    class YourTableViewCell: UITableViewCell {
        weak var delegate: YourTableViewCellDelegate?
    
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
    
            let button = UIButton()
            button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
            addSubview(button)
        }
    
        required init?(coder _: NSCoder) {
            return nil
        }
    
        @objc func buttonTapped() {
            delegate?.customCellDidPressUrlButton(self)
        }
    
    }
    

    Then, in the controller, set itself as a delegate and get the indexPath trough the proper method, indexPath(for:)

    class YourTableViewController: UITableViewController {
    
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! YourTableViewCell
            cell.delegate = self
            return cell
        }
    }
    
    extension YourTableViewController: YourTableViewCellDelegate {
        func customCellDidPressUrlButton(_ yourTableCell: YourTableViewCell) {
            guard let indexPath = tableView.indexPath(for: yourTableCell) else { return }
            print("Link button pressed at \(indexPath)")
        }
    }
    

    Then use that indexPath to grab the correct URL and present it from your table viewcontroller with a SFSafariViewController.

    0 讨论(0)
提交回复
热议问题