'Put specific cells on top of the tableView
How can i put specific cells to show on top of the tableView? , For example I have 10 cells which 5 of them have red color and other 5 have green and I want to show the top 5 ones with the green colors on top and the other 5 with red below them?
I didn't find any resources so I decided to get advise from the dear SO community.
Code:
extension MonitorimiViewController: UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listOfVechicle.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "monitorimiCell", for: indexPath) as? MyCustomCell1 else {return UITableViewCell()}
let currentVechicle = listOfVechicle[indexPath.row]
cell.veturaOutlet.text = currentVechicle.Plate
cell.shpejtsiaAktualeOutlet.text = currentVechicle.Speed!
cell.perditsuarOutlet.text = "\(currentVechicle.LastCommunicationDate!.convertToDisplayForm())"
cell.pasagjereOutlet.text = "\(currentVechicle.Passengers!) Person/a"
cell.vozitesiOutlet.text = "\(currentVechicle.Driver!)"
switch currentVechicle.Status {
case 1:
cell.colorsImageView.backgroundColor = .green
case 2:
cell.colorsImageView.backgroundColor = .red
case 3:
cell.colorsImageView.backgroundColor = .black
case 4:
cell.colorsImageView.backgroundColor = .orange
default: break
}
return cell
So from the currentVechicle.Status I need the case 1 to show on top then the rest.
My data structure coming from the API:
struct Vehicles: Codable {
var IDVehicle: Int?
var Title: String?
var RegistrationDate: String?
var ExpireDate: String?
var Department: String?
var Identification: String?
var Speed: String?
var Latitude: Double?
var Longitude: Double?
var Angle: Int?
var Status: Int?
var InputValue: Int?
var Plate: String?
var LastCommunicationDate: String?
var Passengers: Int?
var Driver: String?
}
data coming from api in form of json:
{
"IDVehicle": 0,
"Title": "Truck",
"RegistrationDate": "2017-10-26T17:23:20.677",
"ExpireDate": "2022-10-28T00:00:00",
"Department": "Transport",
"Identification": "863286023094252",
"Speed": "0",
"Latitude": 80.8769778,
"Longitude": 70.9877813,
"Angle": 265,
"Status": 1, // I want the cells sorted based on the Status, If status 1 show cells with 1 on top etc.
"InputValue": 0,
"Plate": "09 09 KO",
"LastCommunicationDate": "2021-10-28T13:53:23",
"Passengers": 0,
"Driver": "Empty"
}
Solution 1:[1]
struct Vehicles: Codable {
var Title: String
var Status: Int // I'm assuming this will never be null in your JSON. Remove all your question marks except for keys that might not have values.
}
var data = [Vehicles(Title: "Honda", Status: 2), Vehicles(Title: "Mazda", Status: 1), Vehicles(Title: "Subaru", Status: 1)]
// You surely keep your data in an Array like this, right? You will need it formatted like this to be able to index it using `IndexPath.row`
data.sort{$0.Status < $1.Status}
// Call this when you're done populating your data.
The key thing here is in the .map {} part. $0 means THIS element in the list, much like how name in for name in names {} would mean the current name in the list. and $1 means the next element in the list. When using .sort(), you're comparing one value to the next value repeatedly. In this case, we using the < sign because we want the higher values to be further down the list.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
