简体   繁体   English

隐藏表格中的选定单元格-Swift4

[英]Hide Selected Cell from the Table - Swift4

I have a list with 4 objects of places that I query from my Realm database. 我有一个列表,其中包含从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;
    }
))

I'm trying to hide the selected one. 我正在尝试隐藏所选的对象。

Ex. 防爆。 When I click on Daniel Webster Highway , I don't want that to show on my list. 当我单击Daniel Webster Highway ,我不希望其显示在列表中。

在此处输入图片说明

How would one go above and do that in Swift 4 ? 在Swift 4中如何做到这一点?


Code

//
//  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
    }



}

You could pass the index of the selected row from placesVC to your PlaceDetailVC and in 您可以将所选行的索引从placeVC传递到PlaceDetailVC并在

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

    if indexPath.row == passedIndex {
        return 0
    }

    return 70
}

set the cell height to 0 to hide the cell. 将单元格高度设置为0以隐藏该单元格。

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

Then in tableView(_:cellForRow:) 然后在tableView(_:cellForRow:)

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

Just don't do that. 只是不要那样做。 These info need to be synchronized. 这些信息需要同步。

Instead, use another class/struct, or an extension which will hold the distance and the place. 相反,请使用其他类/结构或将保留距离和位置的扩展名。

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?
}

In load() : 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)
}

Now, in tableView(_:cellForRow:) : 现在,在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
}

And keep that logic wherever you want, if the heightForRow if needed (if you want for instance that all your images be at 80pt but the rest at 44pts, etc. 并根据需要将逻辑保持在所需的位置(如果需要)(例如,如果您希望所有图像均为80pt,其余图像均为44pt,依此类推。

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

NB: Code not tested, might not compile, but you should get the idea. 注意:代码未经测试,可能无法编译,但是您应该明白这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM