ios - 从表格中隐藏选定的单元格 - 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
}
}
解决方案
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
}
并在任何你想要的地方保留这个逻辑,如果需要的话heightForRow(例如,如果你想要你的所有图像都在80pt,但其余的在44pts,等等。
在tableView(_:didSelectRowAt:)
、添加tableView.reloadData()
或更好tableView.reloadRows(at: [indexPath] with: .automatic)
注意:代码未经测试,可能无法编译,但您应该明白这一点。
推荐阅读
- spring - LOMBOK 默认字段值不适用于 ORIKA-MAPPER
- python - Oauth 配置文件格式 youtube 到 reddit 视频机器人
- apache-spark - Spark 数据帧分区计数
- azure - 是否有 azure python sdk 来检索应用程序洞察数据?
- python-3.x - 使用 Python 在 PowerBI 上实现自动化
- linux - 在 Linux 中持续监控日志并在发现错误时发送电子邮件?
- .net - 每 4 分钟发送一次保持活动请求时,Azure Functions 在消费计划中仍然“冷启动”
- java - 为什么使用 Windows 7 机器连接到我的服务器 IP 地址会导致连接超时异常?
- scala - scala自我类型特征实例化
- python - TypeError: object() 在尝试 admin.site.register 时不接受任何参数