How to add different links to an array of buttons?

402 views Asked by At

I have a button that is used in view table controller in xcode 3 and I want all the getbutton on the right side to open different links how can I do this.

enter image description here

I have no idea how to do this. I have tried to add an array and access them but it didn't work.

3

There are 3 answers

0
JustinM On

You need to set the tag of the button to the indexPath.row in cellForRow dataSource method.

 cell.button.tag = indexPath.row

then in the buttons selector function you get the cell at that row.

 func buttonTapped(_ sender: UIButton) {
   guard let tappedCell = tableView.cellForRow(at: IndexPath(row: sender.tag, section: 0)) as? CustomCellClass, let url = URL(string: tappedCell.modelObject.urlString) else { return }
  //assuming the model object you assign to each cell has a link property, you can now access the link for that cell
  UIApplication.shared.openURL(url) 
 }
0
Prientus On

This is kind of how I would approach this:

//This is your custom cell class
----------------------------------
protocol CustomCellProtocol {
    func didTapGet(cell:UITableViewCell)
}

class CustomCell:UITableViewCell {

    var delegate:CustomCellProtocol?

    @IBAction func getTapped(id:Any) {
        delegate?.didTapGet(cell: self)
    }
}
//This is your View controller
-----------------------------------
class YourTableViewController:UIViewController, CustomCellProtocol, UITableViewDataSource {
//Your Code here
    var tableView:UITableView?
    let urlArray = ["www.example1.com", "www.example2.com"]

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier") as! CustomCell

        //cell code here
        cell.delegate = self

        return cell
    }

    func didTapGet(cell: UITableViewCell) {
        if let indexPath = tableView?.indexPath(for: cell), let url = URL(string:urlArray[indexPath.row]) {

            UIApplication.shared.open(url, options: [], completionHandler: nil)
        }
    }
}
0
Pragnesh Vitthani On

You should write this code:

var Urlarr = ["http://drivecurrent.com/using-swift-and-avfoundation-to-create-a-custom-camera-view-for-an-ios-app/","https://fabric.io/kits/ios/crashlytics/summary","http://stackoverflow.com/questions/41156065/how-can-i-add-different-links-to-an-array-of-buttons"]

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return Urlarr.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = self.TableView.dequeueReusableCell(withIdentifier: "Cell") as! TableViewCell
    cell.TblButton.tag = indexPath.row
    cell.TblButton.addTarget(self,action:#selector(buttonClicked),
                      for:.touchUpInside)

    return cell
}
func buttonClicked(sender:UIButton)
{
    UIApplication.shared.openURL(NSURL(string: Urlarr[sender.tag]) as! URL)
}