0

I currently have a custom type Cell that I use to populate cells info, style, etc... with. Here's the code

struct Cell {
var opened = Bool()
var isChecked = Bool()
var body = String()
var icon = UIImage()
var cellStyle: UITableViewCell.CellStyle?
var detailTextLabel: String?
var accessoryType: UITableViewCell.AccessoryType?
var action: CellDelegate?
}

I would like to add a property that contains a specific method for each cell. Most solutions I came across suggest using cell's tag and go from there but I can't imagine this being a sustainable solution. Most of those functions are literally to transition from one view to the other i.e. push views. The rest are used to toggle switches, update texts, etc... I'm also open to other ideas on how I can do this. Thanks

Edit: don't get distracted by CellDelegate type, it was basically one of my attempts in trying to get this to work

3
  • 1
    Unclear what the problem is, since this is just a configuration object, not an actual cell in an actual table. You have settable properties that are Bools, Strings, etc. So why not have a settable property that is a function? What’s the difference? If that’s not helpful, show more code rather than this isolated excerpt. Commented Sep 27, 2020 at 17:55
  • @matt I was just trying that now, you could have a generic func action() {} but you can't "override" it or customise it for each cell...can you? Commented Sep 27, 2020 at 17:58
  • 1
    I didn’t say a func, I said a property. Commented Sep 27, 2020 at 17:59

1 Answer 1

1

If your method has always the same signature then:

Swift solution:

add a property to the Cell

var action: (() -> Void)?

Objective-C solution:

add a property to the Cell with selector

var action: Selector? // action = #selector(customMethod) and cell perform a selector

If a method signature varies then you can use Any type

var action: Any?

and cell which calls the action must know the signature. This is done with casting:

class CellView: UITableViewCell {
   private var cell: Cell!
   fun configureWith(_ cell: Cell) {
       self.cell = cell
   }
   override func select(_ sender: Any?) {
      if let action = cell.action as? (String) -> Int {
          action()
      }
   }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that seems to clear out some of the problem. But I still can't seem to be able to add individual func to my Type bc "Cannot use instance member 'toggleDarkModeOff' within property initializer; property initializers run before 'self' is available".
i.e. I'm trying to Toggle Light Mode On by declaring this property let lightModeCell = Cell(cellId: "darkModeOff", isChecked: !ThemeManager.isDarkMode(), body: "Light Mode", icon: UIImage(systemName: "seal.fill")!, action: toggleDarkModeOff()) and then calling it out from didSelectRowAt by tableSections[indexPath.section].sectionData[indexPath.row].action
your are not passing toggleDarkModeOff you are executing it. To pass it just type toggleDarkModeOff without ()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.