Post

Replies

Boosts

Views

Activity

Landmarks Tutorial. Preview doesn't work
Hi everyone, I'm trying to follow the Landmarks project tutorial and got stuck in the second part, in the second section (Create the Row View), step 6 where it says "Modify the text view to use the landmark property’s name." This must be done in the LandmarkRow.swift file. https://developer.apple.com/tutorials/swiftui/building-lists-and-navigation The tutorial shows that after doing this, the preview will display the name of the landmark (Turtle Rock) instead of the usual "Hello world" greeting. But my replacement is not happening. Moreover, from this point on, the preview stops working. There are no error messages in the code, except for the message that the preview could not be performed. I checked and rewrote the code several times, replaced the data source files, but nothing helped. At the same time, the preview works well in other view files. I can't figure out what's wrong with my code? Any ideas as to what the reason will be is appreciated. Below is the code of two files, the LandmarkRow.swift, where view does not work, the second is ModelData.swift and it is related to the previous one. LandmarkRow.swift import SwiftUI struct LandmarkRow: View {     var landmark: Landmark     var body: some View {         HStack {             landmark.image                 .resizable()                 .frame(width: 50, height: 50)             Text(landmark.name)             Spacer()         }     } } struct LandmarkRow_Previews: PreviewProvider {     static var previews: some View {         LandmarkRow(landmark: landmarks[0])     } } ModelData.swift import Foundation var landmarks: [Landmark] = load("landmarkData.json") func loadT: Decodable(_ filename: String) - T {     let data: Data     guard let file = Bundle.main.url(forResource: filename, withExtension: nil)     else {         fatalError("Couldn't find \(filename) in main bundle")     }     do {         data = try Data(contentsOf: file)     } catch {         fatalError("Couldn't load \(filename) from main bundle:\n\(error)")     }     do {         let decoder = JSONDecoder()         return try decoder.decode(T.self, from: data)     } catch {         fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)")     } }
10
1
3.7k
Sep ’23
Checkmarks are not removed from the table cell
I want to mark the items in the list with checkmarks. But some kind of error occurs because the checkmark is installed, but not removed. Why is this happening? This is a Model: import UIKit struct Food: Codable {     var name: String     var isSelected: Bool = false } This is a TableViewController: import UIKit class SelectFoods: UITableViewController {     var foods = [Food]()     override func viewDidLoad() {         super.viewDidLoad()         foods = loadRealOrDemoData() // Load data     }     // MARK: - Table view data source     override func numberOfSections(in tableView: UITableView) -> Int {         return 1     }     override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {         return foods.count     }     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {         let cell = tableView.dequeueReusableCell(withIdentifier: "selectFood", for: indexPath)         let food = foods[indexPath.row]         cell.textLabel?.text = food.name         // Assign checkmark from the selected item to the cell         if food.isSelected {             cell.accessoryType = .checkmark         } else {             cell.accessoryType = .none         }         cell.selectionStyle = .none         return cell     }     // MARK: - Select Food     override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {         // Create variable and check it.         guard let cell = tableView.cellForRow(at: indexPath) else { return }          //Clean cell format before select         cell.accessoryType = .none         // Get an item         var item = foods[indexPath.row]         // Set checkmark         if item.isSelected == false {             cell.accessoryType = .checkmark             item.isSelected.toggle()         // Remove checkmark         } else {             cell.accessoryType = .none             item.isSelected.toggle()         }     } }
Topic: UI Frameworks SubTopic: UIKit Tags:
11
0
2.0k
Oct ’21