簡體   English   中英

隱藏表格中的選定單元格-Swift4

[英]Hide Selected Cell from the Table - Swift4

我有一個列表,其中包含從Realm數據庫查詢的4個場所對象。

Optional(Results<Place> <0x7feaaea447c0> (
    [0] Place {
        name = Daniel Webster Highway;
        country = United States;
        lat = 42.72073329999999;
        lon = -71.44301460000001;
    },
    [1] Place {
        name = District Avenue;
        country = United States;
        lat = 42.48354969999999;
        lon = -71.2102486;
    },
    [2] Place {
        name = Gorham Street;
        country = United States;
        lat = 42.62137479999999;
        lon = -71.30538779999999;
    },
    [3] Place {
        name = Route de HHF;
        country = Haiti;
        lat = 18.6401311;
        lon = -74.1203939;
    }
))

我正在嘗試隱藏所選的對象。

防爆。 當我單擊Daniel Webster Highway ,我不希望其顯示在列表中。

在此處輸入圖片說明

在Swift 4中如何做到這一點?


//
//  PlaceDetailVC.swift
//  Memorable Places
//
//

import UIKit
import CoreLocation
import RealmSwift

class PlaceDetailVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var address: UILabel!
    @IBOutlet weak var placesTable: UITableView!

    var selectedPlace : Place = Place()
    var selectedTrip : Trip = Trip()

    var distances = [ String ]()
    var places : Results<Place>?

    override func viewDidLoad() {
        super.viewDidLoad()

        address.text = selectedPlace.name

        //register xib file
        placesTable.register(UINib(nibName: "PlaceDetailCell", bundle: nil), forCellReuseIdentifier: "customPlaceDetailCell")

    }

    override func viewDidAppear(_ animated: Bool) {

        load()

        if selectedPlace != nil && places != nil {

            for i in 0..<places!.count {

                let latitude = Double(places![i].lat)
                let longitude = Double(places![i].lon)

                let currentLatitude = Double(selectedPlace.lat)
                let currentLongitude = Double(selectedPlace.lon)

                //print(latitude,longitude,currentLatitude,currentLongitude)

                let coordinate = CLLocation(latitude: latitude, longitude: longitude)
                let currentCoordinate = CLLocation(latitude: currentLatitude, longitude: currentLongitude)

                let distanceInMeters = coordinate.distance(from: currentCoordinate) // result is in meters
                let distanceInMiles = distanceInMeters/1609.344

                distances.append(String(format: "%.2f", distanceInMiles))

            }

        }
    }

    // ---------------------------------------------------------------------------------------------------------
    //MARK - CRUD functions


    //Read
    func load() {
        places  = selectedTrip.places.sorted(byKeyPath: "name", ascending: true)
        //print(places,"<<<")
        placesTable.reloadData()

    }


    // ---------------------------------------------------------------------------------------------------------
    //MARK - Table View Datasource

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return places?.count ?? 0
    }

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

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

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

        if selectedPlace.name != nil {

            cell.address.text = (places![indexPath.row]["name"] as! String)
            cell.distance.text = distances[indexPath.row]

        }

        return cell
    }

    // ---------------------------------------------------------------------------------------------------------
    //MARK - Table View Delegate

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        activePlace = indexPath.row
    }



}

您可以將所選行的索引從placeVC傳遞到PlaceDetailVC並在

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

    if indexPath.row == passedIndex {
        return 0
    }

    return 70
}

將單元格高度設置為0以隱藏該單元格。

var distances = [ String ]()
var places : Results<Place>?

然后在tableView(_:cellForRow:)

cell.address.text = (places![indexPath.row]["name"] as! String)
cell.distance.text = distances[indexPath.row]

只是不要那樣做。 這些信息需要同步。

相反,請使用其他類/結構或將保留距離和位置的擴展名。

var array: [PlaceModel]
struct PlaceModel {
    let place: Place
    let distance: Double //You can use String, but that's bad habit
    //Might want to add the "image link" also?
}

load()

array.removeAll()
let tempPlaces = selectedTrip.places.sorted(byKeyPath: "name", ascending: true)
for aPlace in tempPlaces {
    let distance = //Calculate distance for aPlace
    array.append(PlaceModel(place: aPlace, distance: distance)
}

現在,在tableView(_:cellForRow:)

let aPlaceModel = array[indexPath.row]
if activePlace == indexPath {
    let cell = tableView.dequeue...
    //Use cellWithImage for that place
    return cell
} else {
    let cell = tableView.dequeue...
    cell.address.text = aPlaceModel.place.name
    cell.distance.text = aPlaceModel.distance
    return cell
}

並根據需要將邏輯保持在所需的位置(如果需要)(例如,如果您希望所有圖像均為80pt,其余圖像均為44pt,依此類推。

tableView(_:didSelectRowAt:) ,添加tableView.reloadData()或更好的tableView.reloadRows(at: [indexPath] with: .automatic)

注意:代碼未經測試,可能無法編譯,但是您應該明白這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM