Skip to content

Commit 20fa329

Browse files
committed
v0.3.1 Fix longPress bugs caused by collectionViewCell reuse
1 parent a83f31f commit 20fa329

23 files changed

Lines changed: 120 additions & 86 deletions

Example/JZCalendarWeekViewExample/Source/CustomViews/CustomWeekView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class CustomWeekView: JZBaseWeekView {
2323
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
2424
let date = flowLayout.dateForColumnHeader(at: indexPath)
2525
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: EventCell.className, for: indexPath) as! EventCell
26-
cell.updateView(event: allEventsBySection[date]![indexPath.row] as! Event)
26+
cell.configureCell(event: allEventsBySection[date]![indexPath.row] as! Event)
2727
return cell
2828
}
2929

Example/JZCalendarWeekViewExample/Source/DefaultViewController.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ extension DefaultViewController: OptionsViewDelegate {
8787
optionsButton.heightAnchor.constraint(equalToConstant: 25).isActive = true
8888
}
8989
optionsButton.addTarget(self, action: #selector(presentOptionsVC), for: .touchUpInside)
90-
self.navigationController?.navigationBar.tintColor = JZWeekViewColors.today
9190
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: optionsButton)
9291
}
9392

Example/JZCalendarWeekViewExample/Source/DefaultWeekView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class DefaultWeekView: JZBaseWeekView {
1919

2020
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
2121
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: EventCell.className, for: indexPath) as! EventCell
22-
cell.updateView(event: getCurrentEvent(with: indexPath) as! Event)
22+
cell.configureCell(event: getCurrentEvent(with: indexPath) as! Event)
2323
return cell
2424
}
2525

Example/JZCalendarWeekViewExample/Source/Event.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,19 @@ import JZCalendarWeekView
1212
class Event: JZBaseEvent {
1313

1414
var location: String
15-
/// Not used for now
16-
var eventType: Int
1715
var title: String
18-
var id: String
1916

20-
init(id: String, title: String, startDate: Date, endDate: Date, location: String, eventType: Int) {
21-
self.id = id
17+
18+
init(id: String, title: String, startDate: Date, endDate: Date, location: String) {
2219
self.location = location
23-
self.eventType = eventType
2420
self.title = title
25-
super.init(startDate: startDate, endDate: endDate)
21+
22+
// If you want to have you custom uid, you can set the parent class's id with your uid or UUID().uuidString (In this case, we just use the base class id)
23+
super.init(id: id, startDate: startDate, endDate: endDate)
2624
}
2725

2826
override func copy(with zone: NSZone?) -> Any {
29-
return Event(id: id, title: title, startDate: startDate, endDate: endDate, location: location, eventType: eventType)
27+
return Event(id: id, title: title, startDate: startDate, endDate: endDate, location: location)
3028
}
3129

3230

Example/JZCalendarWeekViewExample/Source/EventCell.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class EventCell: UICollectionViewCell {
3434
borderView.backgroundColor = UIColor(hex: 0x0899FF)
3535
}
3636

37-
func updateView(event: Event) {
37+
func configureCell(event: Event) {
3838
self.event = event
3939
locationLabel.text = event.location
4040
titleLabel.text = event.title

Example/JZCalendarWeekViewExample/Source/LongPressViews/LongPressEventCell.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
import UIKit
1010
import JZCalendarWeekView
1111

12-
class LongPressEventCell: JZBaseEventCell {
12+
// If you want to use Move Type LongPressWeekView, you have to inherit from JZLongPressEventCell and update event when you configure cell every time
13+
class LongPressEventCell: JZLongPressEventCell {
1314

1415
@IBOutlet weak var titleLabel: UILabel!
1516
@IBOutlet weak var locationLabel: UILabel!
@@ -19,6 +20,9 @@ class LongPressEventCell: JZBaseEventCell {
1920
super.awakeFromNib()
2021

2122
setupBasic()
23+
// You have to set the background color in contentView instead of cell background color, because cell reuse problems in collectionview
24+
// When setting alpha to cell, the alpha will back to 1 when collectionview scrolled, which means that moving cell will not be translucent
25+
self.contentView.backgroundColor = UIColor(hex: 0xEEF7FF)
2226
}
2327

2428
func setupBasic() {
@@ -29,11 +33,10 @@ class LongPressEventCell: JZBaseEventCell {
2933
layer.shadowOpacity = 0
3034
locationLabel.font = UIFont.systemFont(ofSize: 12)
3135
titleLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
32-
self.backgroundColor = UIColor(hex: 0xEEF7FF)
3336
borderView.backgroundColor = UIColor(hex: 0x0899FF)
3437
}
35-
36-
func updateView(event: Event) {
38+
39+
func configureCell(event: Event) {
3740
self.event = event
3841
locationLabel.text = event.location
3942
titleLabel.text = event.title

Example/JZCalendarWeekViewExample/Source/LongPressViews/LongPressViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ extension LongPressViewController: JZLongPressViewDelegate, JZLongPressViewDataS
7878

7979
func weekView(_ weekView: JZLongPressWeekView, didEndAddNewLongPressAt startDate: Date) {
8080
let newEvent = Event(id: UUID().uuidString, title: "New Event", startDate: startDate, endDate: startDate.add(component: .hour, value: weekView.addNewDurationMins/60),
81-
location: "Melbourne", eventType: 5)
81+
location: "Melbourne")
8282

8383
if viewModel.eventsByDate[startDate.startOfDay] == nil {
8484
viewModel.eventsByDate[startDate.startOfDay] = [Event]()

Example/JZCalendarWeekViewExample/Source/LongPressViews/LongPressWeekView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class LongPressWeekView: JZLongPressWeekView {
1919

2020
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
2121
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: LongPressEventCell.className, for: indexPath) as! LongPressEventCell
22-
cell.updateView(event: getCurrentEvent(with: indexPath) as! Event)
22+
cell.configureCell(event: getCurrentEvent(with: indexPath) as! Event)
2323
return cell
2424
}
2525

Example/JZCalendarWeekViewExample/Source/RootViewModel.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ class RootViewModel: NSObject {
1515
private let secondDate = Date().add(component: .day, value: 1)
1616
private let thirdDate = Date().add(component: .day, value: 2)
1717

18-
lazy var events = [Event(id: "0", title: "One", startDate: firstDate, endDate: firstDate.add(component: .hour, value: 1), location: "Melbourne", eventType: 0),
19-
Event(id: "1", title: "Two", startDate: secondDate, endDate: secondDate.add(component: .hour, value: 4), location: "Sydney", eventType: 0),
20-
Event(id: "2", title: "Three", startDate: thirdDate, endDate: thirdDate.add(component: .hour, value: 2), location: "Tasmania", eventType: 1),
21-
Event(id: "3", title: "Four", startDate: thirdDate, endDate: thirdDate.add(component: .day, value: 1), location: "Canberra", eventType: 1)]
18+
lazy var events = [Event(id: "0", title: "One", startDate: firstDate, endDate: firstDate.add(component: .hour, value: 1), location: "Melbourne"),
19+
Event(id: "1", title: "Two", startDate: secondDate, endDate: secondDate.add(component: .hour, value: 4), location: "Sydney"),
20+
Event(id: "2", title: "Three", startDate: thirdDate, endDate: thirdDate.add(component: .hour, value: 2), location: "Tasmania"),
21+
Event(id: "3", title: "Four", startDate: thirdDate, endDate: thirdDate.add(component: .hour, value: 26), location: "Canberra")]
2222

2323
lazy var eventsByDate: EventsByDate = JZWeekViewHelper.getIntraEventsByDate(originalEvents: events)
2424

Example/JZCalendarWeekViewExample/Supporting Files/Info.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
<key>CFBundlePackageType</key>
1616
<string>APPL</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>0.3.0</string>
18+
<string>0.3.1</string>
1919
<key>CFBundleVersion</key>
20-
<string>1805031223</string>
20+
<string>1805041708</string>
2121
<key>LSRequiresIPhoneOS</key>
2222
<true/>
2323
<key>UILaunchStoryboardName</key>

0 commit comments

Comments
 (0)