Skip to content

Instantly share code, notes, and snippets.

@kostapappas
Last active November 5, 2018 17:58
Show Gist options
  • Save kostapappas/e72b60bc20c866c9bb2e5f2a85289ef3 to your computer and use it in GitHub Desktop.
Save kostapappas/e72b60bc20c866c9bb2e5f2a85289ef3 to your computer and use it in GitHub Desktop.
custom UITableViewCell
final class MyCell: UITableViewCell {
final let myCustomView: UIView
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
self.myCustomView = UIView()
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.contentView.addSubview(self.myCustomView)
self.initializeBasicElements()
}
// Deserialize your object here
// required = subclasses of that class have to implement it too
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
//its called when
//Its own bounds (not frame) changed
//The bounds of one of its direct subviews changed
//A subview is added to the view or removed from the view
//set the frame rectangles of your subviews directly
//setNeedsLayout(): If you want to force a layout update, call the setNeedsLayout() method instead to do so prior to the next drawing update.
//layoutIfNeeded(): If you want to update the layout of your views immediately, call the layoutIfNeeded() method
//use both setNeedsLayouts - layoutIfNeeded for instant use
final override func layoutSubviews() {
super.layoutSubviews()
self.myCustomView.frame = self.contentView.bounds
// intrinsic content size changes
// super.layoutSubviews() The second call to super.layoutSubviews() is optional but may be required
//if the intrinsic conent size of the view changes
}
//For performance reasons, you should only reset attributes of the cell
//that are not related to content
//for example, alpha, editing, and selection state.
final override func prepareForReuse() {
super.prepareForReuse()
self.initializeBasicElements()
}
//The table view'€™s delegate in tableView(_:cellForRowAt:)
//should always reset all content when reusing a cell.
//use cell.configure(mynewRowDataHere)
final func configure(/*userID: Int*/) {
//do the setup here
self.setNeedsLayout()
}
private func initializeBasicElements () {
self.movieImage.image = nil
self.self.movieTitle.text = ""
}
}
@kostapappas
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment