Skip to content

7. TableView Cells & CollectionView Cells

S4cha edited this page Jan 8, 2017 · 1 revision

For both tableView cells and UICollectionView cells, sv adds the subviews to the contentView, as recommended.

Example

class FriendCell: UITableViewCell {

    let avatar = UIImageView()
    let name = UILabel()

    required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder)}
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        sv(
            avatar,
            name.style(nameStyle)
        )

        avatar.size(50).centerVertically()
        alignHorizontally(|-20-avatar-name-20-|)
    }

    func nameStyle(l:UILabel) {
        l.font = .systemFontOfSize(24)
        l.textColor = .blueColor()
    }
}

Then in the viewController you do the usual register and dequeue :)

// In viewDidLoad, register your cell for dequeue
tableView.registerClass(FriendCell.self, forCellReuseIdentifier: "FriendCell")

// Later, in cellForRowAtIndexPath
let cell = tableView.dequeueReusableCellWithIdentifier("FriendCell", forIndexPath: indexPath) as! FriendCell