0

I apologize if this question have already been answered. I tried but couldn't find an answer.

I want to edit the "rating:Int" value from the custom UITableViewCell. The ITableView that get it's data from TableData show below:

class HomeViewController: UIViewController, UITableViewDataSource {    
    @IBOutlet weak var tableView: UITableView!
    var TableData:Array< Post > = Array < Post >()
    var PostTableData:Array <NSMutableDictionary> = Array <NSMutableDictionary> ()
    let cellSpacingHeight: CGFloat = 5

Each cell in this table has two buttons to increase or decrease rating value in the Post object from "var TableData:Array< Post > = Array < Post >()". However, I cannot figure out how to access the Post object in the

@IBAction func decreaseRating(_ sender: Any) {

I am not trying to access the cell, but var TableData:Array< Post > = Array < Post >() that is sourcing the tableview.

Any advice and help will be appreciated.

health-dev
  • 133
  • 1
  • 7
  • 1
    Possible duplicate of [which cell button has pressed in tableView in swift 3](https://stackoverflow.com/questions/43588416/which-cell-button-has-pressed-in-tableview-in-swift-3) – Alex Ioja-Yang Aug 18 '17 at 14:37
  • Thanks for the quick response. Unfortunately, that post only gives me access to the cell, while I can to get access to TableData:Array which is used to populate these cells. – health-dev Aug 18 '17 at 15:32
  • 1
    And, unfortunately, none of the answers for that question are very good. Rather than using `tag`, I'd suggest moving `decreaseRating` into your `UITableViewCell` subclass (if it's not already there; you weren't clear where you implemented this), and then set up a delegate-protocol pattern between the cell and the table view data source, one in which the cell passes the cell reference. The table view data source can then use that cell reference to figure out the `IndexPath` within the table. And once you have the index path, you can use its `row` to know which model array entry to update. – Rob Aug 18 '17 at 15:34
  • In your `tableView cell for row at indexPath` you assign your `button.tag = indexPath.row`. When `decreaseRating(_ sender :**UIButton**)` gets called you use `sender.tag` to determine which element in TableData you want to access. So `TableData[sender.tag] -= 1` – Alex Ioja-Yang Aug 18 '17 at 15:38
  • Using `tag` for this purpose is not a good idea. – Rob Aug 18 '17 at 15:39
  • 1
    Two suitable solutions using a custom `UITableViewCell`: 1) Pass the `Post` item in `cellForRow` to the cell. Due to reference semantics (assuming `Post` is a class) the changes will persist 2) Use a callback closure in `cellForRow` and handle the action in the callback. And – as always – do not use `NSMutable...` collection types in Swift. You throw away the type information of the containing items. – vadian Aug 18 '17 at 15:39
  • 2
    I think @vadian second option is great approach, too. First is OK only if `Post` is reference type (i.e. `class` and not `struct`). If you want protocol-delegate pattern, here is an example of that: https://stackoverflow.com/a/39566554/1271826 – Rob Aug 18 '17 at 15:42
  • Thank you every one for your prompt comments. This has been very helpful. – health-dev Aug 20 '17 at 16:44

0 Answers0