Reputation: 442
I want to ovrride init(frame: CGRect)
in a subclassed UITableViewCell.
override init(frame: CGRect) {
super.init(frame: CGRect)
}
But this throws an error: Initializer does not ovrride a designated initializer from its super class
, I did something wrong?
Upvotes: 11
Views: 19000
Reputation: 330
These are the init
functions you need to override in your child [UITableView][1]
override init(frame: CGRect, style: UITableView.Style) {
//When you manually initializing a table
super.init(frame: frame, style: style)
}
required init?(coder: NSCoder) {
//From xib or storyboard
super.init(coder: coder)
}
Upvotes: 0
Reputation: 516
Case 1: if you need to directly change the height of the cell,you can use
func tableView(_ tableView: UITableView, heightForRowAt indexPath:IndexPath) -> CGFloat {
return 50
}
Case 2: If you need to subclass a cell,go to that cell class and override the reuseidentifier
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
let hrt = heightAnchor.constraint(equalToConstant: 50)
hrt.isActive = true
}
Upvotes: 0
Reputation: 1679
If you really want to use init(frame: CGRect)
, use UICollectionViewCell
. And set the size of your cell with CGSize(width: view.frame.width, height: 100)
. For example,
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: 100)
}
It looks like tableViewCell and you also can use init(frame: CGRect)
.
Upvotes: 0
Reputation: 1786
For an UITableViewCell
, you should be overriding this one:
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
See documentation.
If you would like to be able to vary cells' frame, you should look into delegate methods like:
tableView(_:heightForRowAtIndexPath:)
for row heighttableView(_:indentationLevelForRowAtIndexPath:)
for row indentSee documentation for UITableViewDelegate
.
Upvotes: 16
Reputation: 2748
Yes, UITableViewCell has only two initializers
public init(style: UITableViewCellStyle, reuseIdentifier: String?)
public init?(coder aDecoder: NSCoder)
Upvotes: 0
Reputation: 1163
init(frame: CGRect)
is not an initializer of the superclass UITableViewCell. You need to override init(coder: aDecoder)
or init(style: UITableViewCellStyle, reuseIdentifier: String?)
Upvotes: 0