-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUICollectionView
144 lines (113 loc) · 5.41 KB
/
UICollectionView
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import UIKit
class User {
var username: String
var profilePhoto: UIImage
var rating: Double
var numberOfPhotos: Int
var numberOfDialogs: Int
var numberOfFriends: Int
var dialogs: [Dialog] // add an array of dialogs
init(username: String, profilePhoto: UIImage, rating: Double, numberOfPhotos: Int, numberOfDialogs: Int, numberOfFriends: Int, dialogs: [Dialog]) {
self.username = username
self.profilePhoto = profilePhoto
self.rating = rating
self.numberOfPhotos = numberOfPhotos
self.numberOfDialogs = numberOfDialogs
self.numberOfFriends = numberOfFriends
self.dialogs = dialogs
}
}
class Dialog {
var participants: [User]
var duration: Int // in minutes
var isContinued: Bool
var ratings: [Double] // anonymous ratings
init(participants: [User], duration: Int, isContinued: Bool, ratings: [Double]) {
self.participants = participants
self.duration = duration
self.isContinued = isContinued
self.ratings = ratings
}
}
class UserProfileViewController: UIViewController {
var user: User
init(user: User) { // add a constructor
self.user = user
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
// setup UI with user's information
// ...
}
// function for calculating user's overall rating based on ratings from dialogs
func calculateOverallRating() -> Double {
let totalRatings = user.numberOfDialogs * 5 // assuming all dialogs have 5 ratings
var sumOfRatings = Double(user.numberOfDialogs) * user.rating // assuming all ratings are equal to user's rating if not rated yet
for rating in user.dialogs.flatMap({$0.ratings}) { // get all ratings from all dialogs
sumOfRatings += rating
}
return sumOfRatings / Double(totalRatings)
}
}
class DialogCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var participantsLabel: UILabel!
@IBOutlet weak var durationLabel: UILabel!
@IBOutlet weak var ratingLabel: UILabel!
func configure(with dialog: Dialog) {
participantsLabel.text = dialog.participants.map({$0.username}).joined(separator: ", ")
durationLabel.text = "\(dialog.duration) min"
let averageRating = dialog.ratings.reduce(0, +) / Double(dialog.ratings.count)
ratingLabel.text = String(format: "%.1f", averageRating)
}
}
class UserProfileCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var profilePhotoImageView: UIImageView!
@IBOutlet weak var usernameLabel: UILabel!
@IBOutlet weak var ratingLabel: UILabel!
@IBOutlet weak var numberOfPhotosLabel: UILabel!
@IBOutlet weak var numberOfDialogsLabel: UILabel!
@IBOutlet weak var numberOfFriendsLabel: UILabel!
func configure(with user: User) {
profilePhotoImageView.image = user.profilePhoto
usernameLabel.text = user.username
ratingLabel.text = String(format: "%.1f", user.rating)
numberOfPhotosLabel.text = "\(user.numberOfPhotos) photos"
numberOfDialogsLabel.text = "\(user.numberOfDialogs) dialogs"
numberOfFriendsLabel.text = "\(user.numberOfFriends) friends"
}
}
class MyCollectionViewDataSource: NSObject, UICollectionViewDataSource {
let user: User = User(username: "John Doe", profilePhoto: UIImage(), rating: 4.5, numberOfPhotos: 10, numberOfDialogs: 3, numberOfFriends: 20, dialogs: [Dialog]())
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// Повертає кількість елементів в секції колекції
return 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// Код для налаштування та повернення комірки колекції
return UICollectionViewCell()
}
}
var dialogs: [Dialog] = [ Dialog(participants: [User](), duration: 30, isContinued: false, ratings: [4, 5, 3]),
Dialog(participants: [User](), duration: 60, isContinued: true, ratings: [4, 3, 4, 5, 2]),
Dialog(participants: [User](), duration: 45, isContinued: false, ratings: [5, 5, 5])
]
class MyCollectionView: UICollectionView, UICollectionViewDataSource, UICollectionViewDelegate {
override func awakeFromNib() {
super.awakeFromNib()
dataSource = self
delegate = self
register(UINib(nibName: "UserProfileCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "UserProfileCollectionViewCell")
register(UINib(nibName: "DialogCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "DialogCollectionViewCell")
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 0 // повернути кількість елементів в секції
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
return UICollectionViewCell() // повернути створену клітинку для заданого індексу
}
// додаткові методи для обробки подій делегатів
}