【问题标题】:Open URL with a button inside a table view cell在表格视图单元格内使用按钮打开 URL
【发布时间】:2018-12-13 09:39:50
【问题描述】:

我想在每个打开 URL 的表格单元格中包含一个按钮。

我已经创建了带有图像和标签的表格(使用数组),但是我对如何创建按钮感到困惑

这是我目前所拥有的

class ExploreCell: UITableViewCell {

@IBOutlet weak var exploreImageView: UIImageView!
@IBOutlet weak var exploreTitleView: UILabel!
@IBOutlet weak var exploreDescriptionView: UILabel!
@IBOutlet weak var exploreButton: UIButton!

func setExplore(explore: Explore) {
    exploreImageView.image = explore.image
    exploreTitleView.text = explore.title
    exploreDescriptionView.text = explore.description
    exploreButton.addTarget(self, action: "connected:", for: .touchUpInside) = explore.button

}

我的数组类如下所示

class ExploreListScreen: UIViewController {

@IBOutlet weak var tableView: UITableView!

var explores: [Explore] = []

override func viewDidLoad() {
    super.viewDidLoad()
    explores = createArray ()

    tableView.delegate = self
    tableView.dataSource = self
}

func createArray() -> [Explore] {

    var tempExplores: [Explore] = []

    let explore1 = Explore(image: #imageLiteral(resourceName: "test"), title: "Demo", description: "Essential", button: "")

    tempExplores.append(explore1)

    return tempExplores
}

我终于有了另一个包含声明变量的文件

class Explore {

var image: UIImage
var title: String
var description: String
var button: UIButton

init(image: UIImage, title: String, description: String, button: UIButton) {
    self.image = image
    self.title = title
    self.description = description
    self.button = button
}

任何建议和指导都会很棒。谢谢!

【问题讨论】:

  • 您想设计按钮或在按钮点击中导航不同的网址
  • 我想导航到“createArray”函数中指定的 URL。
  • 在这个 URL 上了解自定义单元格:) medium.com/@aapierce0/…

标签: ios swift


【解决方案1】:

这是我通常如何解决这个问题的。为您的UITableViewCell 子类创建一个委托,并将拥有 tableView 的视图控制器设置为其委托。为单元格内发生的交互添加方法。

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)
    }

}

然后,在控制器中,将自己设置为委托,并通过正确的方法获取 indexPath,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)")
    }
}

然后使用该 indexPath 获取正确的 URL,并使用 SFSafariViewController 从您的 table viewcontroller 中显示它。

【讨论】:

    【解决方案2】:

    斯威夫特 4

    这是使用touchPoint 获取indexPath 的最佳方式

    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

    【讨论】:

    • 这是 a 方式,但不是最好的方式?
    • 最好的方法是什么,请您分享一下,我真的很想详细了解这一点。谢谢
    • 请看我的回答。
    • 是的,这很好,但它怎么可能是最好的方法呢?请解释一下,学习新东西对我来说真的很棒
    【解决方案3】:

    只需在viewDidLoad 中创建UIButton 对象并将此按钮添加为cellForRowAtIndexPath 函数中单元格的子视图。根据您的要求采用 Burton 的框架。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-18
      • 1970-01-01
      相关资源
      最近更新 更多