-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmptyChannelTableViewCell.swift
125 lines (101 loc) · 4.84 KB
/
EmptyChannelTableViewCell.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// EmptyChannelTableViewCell.swift
// TeeVee
//
// Created by Jerry on 3/29/16.
// Copyright © 2016 SmartStream. All rights reserved.
//
import UIKit
protocol EmptyChannelDelegate: class {
func emptyChannel(emptyChannel: EmptyChannelTableViewCell, didUpdateSelectedChannels channels: [String])
}
class EmptyChannelTableViewCell: UITableViewCell {
@IBOutlet weak var collectionView: UICollectionView!
var featuredChannels: [Channel]? {
didSet {
collectionView.reloadData()
}
}
private let bgColor = Theme.Colors.BackgroundColor.color
private let cellID = "com.teevee.ChannelEmptyCell"
var selectedChannels = [String]()
var selectedChannelIndex = [Int: Int]()
weak var delegate: EmptyChannelDelegate?
override func awakeFromNib() {
super.awakeFromNib()
backgroundColor = bgColor
// Initialization code
collectionView.delegate = self
collectionView.dataSource = self
let channelCellNIB = UINib(nibName: "ChannelEmptyCell", bundle: NSBundle.mainBundle())
collectionView.registerNib(channelCellNIB, forCellWithReuseIdentifier: cellID)
collectionView.backgroundColor = bgColor
collectionView.allowsMultipleSelection = true
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
extension EmptyChannelTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
private var infoViewHeight: CGFloat { return 33.0 }
private var imageMargin: CGFloat { return 14.0 }
private var imageColumns: CGFloat { return 2.0 }
private var imageInnerMargin: CGFloat { return 14.0 }
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if let channels = featuredChannels {
return channels.count
} else {
return 0
}
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellID, forIndexPath: indexPath) as! ChannelEmptyCell
if let channels = featuredChannels {
cell.channel = channels[indexPath.item]
if indexPath.item == selectedChannelIndex[indexPath.item] {
cell.isSelected = 0
} else {
cell.isSelected = 2
}
}
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if let channels = featuredChannels {
let channel = channels[indexPath.item]
selectedChannels.append(channel.channel_id!)
selectedChannelIndex[indexPath.item] = indexPath.item
delegate?.emptyChannel(self, didUpdateSelectedChannels: selectedChannels)
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! ChannelEmptyCell
cell.isSelected = 0
}
}
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
if selectedChannels.count > 0 {
if let channels = featuredChannels {
let selected = channels[indexPath.item]
for (index, channelID) in selectedChannels.enumerate() {
if selected.channel_id == channelID {
selectedChannels.removeAtIndex(index)
delegate?.emptyChannel(self, didUpdateSelectedChannels: selectedChannels)
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! ChannelEmptyCell
cell.isSelected = 1
selectedChannelIndex[indexPath.item] = nil
return
}
}
}
}
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let size = (bounds.width - imageMargin*2 - ((imageColumns - 1)*imageInnerMargin)) / imageColumns
return CGSizeMake(size, size+infoViewHeight)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return imageInnerMargin
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return imageInnerMargin
}
}