Last active
August 29, 2015 14:22
-
-
Save algal/6680517f1a6dfc5e4361 to your computer and use it in GitHub Desktop.
Broken generic table view cell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Not sure why this generic version does not work. | |
private class WrapperTableViewCell<T:UIView> : UITableViewCell | |
{ | |
class var classReuseIdentifier:String { return T.self.description() + "CellIdentifier" } | |
override class func requiresConstraintBasedLayout() -> Bool { return true } | |
override init(style: UITableViewCellStyle, reuseIdentifier: String?) | |
{ | |
super.init(style: UITableViewCellStyle.Default, reuseIdentifier: reuseIdentifier) | |
println("I am never called!. why?") | |
self.selectionStyle = .None | |
let containedView = T(frame: self.contentView.bounds) | |
self.contentView.addSubview(containedView) | |
containedView.setTranslatesAutoresizingMaskIntoConstraints(false) | |
for vfl in ["V:|[v]|","H:|[v]|"] | |
{ | |
let cs = NSLayoutConstraint.constraintsWithVisualFormat(vfl, options: .allZeros, metrics: nil, views: ["v":containedView]) as! [NSLayoutConstraint] | |
self.contentView.addConstraints(cs) | |
} | |
} | |
required init(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
var containedView:T { return self.contentView.subviews.first as! T } | |
} | |
/* | |
In my UITableViewController subclass, I would expect to do | |
class MyTVC : UITableViewController { | |
typealias MyCell = WrapperTableViewCell<MyView> | |
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell | |
{ | |
let cell = tableView.dequeueReusableCellWithIdentifier(AppAprrovalCell.classReuseIdentifier, forIndexPath: indexPath) as! AppAprrovalCell | |
cell.containedView.configureWithData( /* etc */ ) | |
return cell | |
} | |
} | |
But when I do this, I get a nil-unwrapping error from the cast to AppApprovalCell. | |
It seems like the override initializer is not being called. | |
*/ | |
// and this collection view version might start working one day: | |
final public class GenericViewWrappingCollectionViewCell<T:UIView> : UICollectionViewCell | |
{ | |
final public class var classReuseIdentifier:String { | |
return T.self.description() + "CellIdentifier" | |
} | |
override public class func requiresConstraintBasedLayout() -> Bool { return true } | |
override init(frame: CGRect) { | |
super.init(frame:frame) | |
setup() | |
} | |
required public init(coder aDecoder: NSCoder) { | |
super.init(coder:aDecoder) | |
setup() | |
} | |
final private func setup() | |
{ | |
let wrappedView = T(frame: self.contentView.bounds) | |
self.contentView.addSubview(wrappedView) | |
wrappedView.setTranslatesAutoresizingMaskIntoConstraints(false) | |
NSLayoutConstraint.activateConstraints(["V:|[v]|","H:|[v]|"].flatMap({NSLayoutConstraint.constraintsWithVisualFormat($0, options: .allZeros, metrics: nil, views: ["v":wrappedView]) as! [NSLayoutConstraint]})) | |
} | |
/** returns the wrapped view. */ | |
final public var wrappedView:T { | |
return self.contentView.subviews.first as! T | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
eridius [2:08 PM]
@alexisgallagher: I think your problem is that generic classes don't get
@objc
methods (edited)