Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Multiple layout with UITableViewCell

yashigani
November 30, 2015

Multiple layout with UITableViewCell

関モバ #8

yashigani

November 30, 2015
Tweet

More Decks by yashigani

Other Decks in Programming

Transcript

  1. .VMUJQMFMBZPVUXJUI
    6*5BCMF7JFX$FMM
    @yashigani
    ؔϞό #8

    View Slide

  2. yashigani
    id:yashigani_w @yashigani
    Mobile Application Engineer
    http://yashigani.hatenablog.com
    Hatena

    View Slide

  3. View Slide

  4. ͋Δ೔ͷͰ͖͝ͱ

    View Slide

  5. View Slide

  6. View Slide

  7. ඍົʹҧ͏ʜ

    View Slide

  8. ͍͔ʹͯ͠ෳ਺ͷ

    ϨΠΞ΢τΛ࣮ݱ͢Δ͔

    View Slide

  9. • ίʔυͰϨΠΞ΢τΛ͍͡Δ
    • xibΛෳ਺༻ҙͯ͠࢖͍෼͚Δ

    View Slide

  10. ίʔυͰ͍͡Δ࡞ઓ

    View Slide

  11. • Pros
    • ΍Δ͜ͱࣗମ͸୯७
    • Cons
    • ফ͑ΔView໰୊
    • Auto Layoutͷઃఆ͕೉͍͠
    • ϨΠΞ΢τͷશ༰Λ௫Έʹ͍͘

    View Slide

  12. ෳ਺ͷYJCΛ࢖͏࡞ઓ

    View Slide

  13. • Pros
    • NSLayoutConstraintΛϓϩύςΟʹ͠ͳͯ͘Α͍
    • Auto LayoutͷࢦఆΛ୯७ʹͰ͖Δ
    • viewࣗମͷϓϩύςΟΛ࢖͍෼͚Ͱ͖Δ
    • Cons
    • ෳ਺ͷxibΛϝϯςφϯε͢Δඞཁ͕͋Δ
    • ܾΊࣄ͕૿͑Δ(࢖͍ճ͠ʹ͘͞)

    View Slide

  14. ࣮૷ύλʔϯ

    View Slide

  15. class EntryCell: UITableViewCell {
    @IBOutlet
    weak var titleLabel: UILabel!
    @IBOutlet
    weak var thumbnailView:UIImageView?
    var entry: Entry!
    }

    View Slide

  16. protocol EntryCellRepresentable {
    var tableView: UITableView! { get }
    func registerEntryCell()
    func dequeueEntryCell(entry: Entry,
    forIndexPath indexPath: NSIndexPath)
    -> EntryCell
    }

    View Slide

  17. extension EntryCellRepresentable {
    func registerEntryCell() {
    let ids = ["EntryCell", "EntryCellWithImage"]
    ids.forEach {
    let nib = UINib(nibName: $0, bundle: nil)
    tableView.registerNib(nib,
    forCellReuseIdentifier: $0)
    }
    }
    func dequeueEntryCell(entry: Entry, forIndexPath

    indexPath: NSIndexPath) -> EntryCell {
    let id = entry.hasImage ? "EntryCellWithImage"
    : "EntryCell"
    let cell =

    tableView.dequeueReusableCellWithIdentifier(id,

    forIndexPath: indexPath) as! EntryCell
    cell.entry = entry
    return cell
    }
    }

    View Slide

  18. ·ͱΊ

    View Slide

  19. • xibΛ෼͚ΔͱAuto LayoutҎ֎΋࢖͍෼͚Ͱ͖Δ
    • OptionalΛ্खʹ࢖͓͏
    • protocol extension࠷ߴ
    • UIStackViewૣ͘࢖ΘͤΖ

    View Slide

  20. ͋Γ͕ͱ͏͍͟͝·ͨ͠

    View Slide