I have made some icon with the paint code app:
class addIconView: UIView {
override func draw(_ rect: CGRect) {
TodayIcon.draw(frame: rect)
}
}
I also add the configuration of the cells in another class:
class OptionsTableCell {
var icon: UIView
var label: String
init(icon: UIView, label: String) {
self.icon = icon
self.label = label
}
}
Then I added a UIView in the prototype cell in a TableView. I used this array to update icons of the cell:
var optionsArray: [OptionsTableCell] = []
func createOptionsArray() -> [OptionsTableCell] {
var cell: [OptionsTableCell] = []
let addIcon = OptionsTableCell(icon: addIconView(), label: "Add")
cell.append(addIcon)
return cell
}
I just added addIconView() to update the icon of the cell. I think it's wrong.
How can I update the custom class of an UIView to change the icon inside it?
Instead of using a generic
UIViewin the cell, subclass it into a new class that encapsulates the types of icons to be displayed. Consider this example:UIViewsubclass that encapsulates the icon logic around PaintCode. Notice theOptionenum and the@IBDesignableproperty (allows for live-rendering inInterface Builder):Storyboard configuration: custom
UITableViewController+ customUITableViewCellwith the customUIView(notice theclassattribute of typeOptionsView):Connect the
UILabeland theOptionsViewto yourCustomCell. Implementation example (notice thevar option):Finally, in your custom
UITableViewController:Final result:
Refer to this project for the entire code (sixth test):
https://github.com/backslash-f/paintcode-tests