Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// // TripsViewController.swift // TravelItineraryapp // // Created by Keerthi Lakshmi Harikrishnan on 12/13/23. // import UIKit class TripsViewController: UIViewController { @IBOutlet var tableView: UITableView!

//

// TripsViewController.swift

// TravelItineraryapp

//

// Created by Keerthi Lakshmi Harikrishnan on 12/13/23.

//

import UIKit

class TripsViewController: UIViewController {

@IBOutlet var tableView: UITableView!

@IBOutlet weak var addButton: FloatingActionButton!

override func viewDidLoad() {

super.viewDidLoad()

tableView.dataSource = self

tableView.delegate = self // Set the delegate to self

// Assuming TripFunctions.readTrips is an asynchronous operation

// that loads the trip models

TripFunctions.readTrips(completion: { [weak self] in

// Ensure self is available and reload the table view on the main thread

DispatchQueue.main.async {

self?.tableView.reloadData()

}

})

view.backgroundColor = Theme.background

addButton.createFloatingActionButton()

}

override func prepare (for segue: UIStoryboardSegue,sender: Any?){

if segue.identifier == "toAddTripSegue" {

let popup = segue.destination as! AddTripViewController

popup.doneSaving = { [weak self] in

self?.tableView.reloadData()

}

}

}

}

extension TripsViewController: UITableViewDataSource, UITableViewDelegate {

// MARK: - UITableViewDataSource Methods

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return Data.tripModels.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TripsTableViewCell

let trip = Data.tripModels[indexPath.row]

cell.setup(tripModel: trip)

return cell

}

}

// Setup the cell with the trip model

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

return 161

}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {

if editingStyle == .delete {

// Handle the delete action here

// Retrieve the TripModel to delete

let tripToDelete = Data.tripModels[indexPath.row]

// Perform delete

TripFunctions.deleteTrip(tripModel: tripToDelete)

// Remove the trip from your data source and update the table

Data.tripModels.remove(at: indexPath.row)

tableView.deleteRows(at: [indexPath], with: .automatic)

}

}

my swipe delete is not working in my stimulator

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions