0

I have an array of data that is taken from the site and output to the table, I want to add a button that will store a certain row from the table in the database how can I implement it?

   let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)
   cell.textLabel?.text = news[indexPath.row].tittle

   let tipDouble = NumberFormatter().number(from: news[indexPath.row].time!)!.doubleValue
   let date = Date(timeIntervalSince1970: tipDouble)

   let dateFormatter = DateFormatter()
   dateFormatter.timeZone = TimeZone(abbreviation: "GMT+2") //Set timezone that you want
   dateFormatter.locale = NSLocale.current
   dateFormatter.dateFormat = "MM-dd HH:mm" //Specify your format that you want

   let strDate = dateFormatter.string(from: date)

   cell.detailTextLabel?.text = strDate

   return cell
picciano
  • 22,341
  • 9
  • 69
  • 82
  • Where do you want the button, in each cell? – koen Feb 19 '20 at 19:45
  • Unrelated, I would move the `NumberFormatter` and `DataFormatter` code outside `cellForRowAt` into your datamodel code. – koen Feb 19 '20 at 19:47
  • https://stackoverflow.com/questions/28659845/swift-how-to-get-the-indexpath-row-when-a-button-in-a-cell-is-tapped/38941510#38941510 – Paulw11 Feb 19 '20 at 19:51
  • @koen I want to put a button in each line and when you click on the button for example in the second line the second line is saved – Вася Тарас Feb 19 '20 at 19:53

3 Answers3

1

Use Tag. set a tag to button. Give Tag value as index path.row value. Then on Button click you can access the corresponding data(if array) at index.

Ex:

var arrData = ["5","6","7","8"]

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

   cell.button.tag = IndexPath.row

}

func buttonAction(sender:UIButton){

   let data = arrData[sender.tag]

}
Tom
  • 513
  • 2
  • 5
  • 20
Antony
  • 11
  • 2
0

The most easy and simple workaround is to set the button tag property as the row number and in the button action get the button tag to do your stuff i.e.

In your tableView(:cellForRowAt:indexPath) add the button tag as row number:

let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath) as! YourTableViewClass
cell.[buttonName].tag = indexPath.row (In case you are just using the single section)

And then in your button action in viewController use the tag of button to track the row i.e.

@IBAction func buttonTapped(sender: UIButton) {
    let row = sender.tag
    // Do your stuff
}
Najeeb ur Rehman
  • 403
  • 4
  • 11
  • 1
    There's a problem with this. The row number of a cell can change. For example, if you remove a row before it, or insert a new road. You should store something that lets you identify the cell within your data source, independent of changes. – gnasher729 Feb 19 '20 at 21:07
  • 1
    Yes, using `tag` isn't the best approach - See https://stackoverflow.com/questions/28659845/swift-how-to-get-the-indexpath-row-when-a-button-in-a-cell-is-tapped/38941510 for some more sophisticated approaches – Paulw11 Feb 20 '20 at 02:32
  • Yes, it is not the best approach but for simple cases you use that way as it is really simple and easy. – Najeeb ur Rehman Feb 23 '20 at 17:43
0

Yes Exactly If going through delete and insert. But there is a solution for this.

var arrData: dataTag = [dataTag]()

class dataTag {
    var name:String?
    .......

    var tag: Int? =  Date().IntegerValue. Here "anyUniqueEntry value". 
}



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
cell.button.tag = IndexPath.row
..

}
func buttonAction(sender:UIButton){

let data = arrData[sender.tag]

...

}
iOSDev
  • 199
  • 11
Antony
  • 11
  • 2