【发布时间】:2018-11-06 19:58:02
【问题描述】:
我有
包含这些内容的地方
print(places)
Optional(Results<Place> <0x109526f30> (
[0] Place {
name = 127 Pine St;
country = United States;
lat = 42.63341130000001;
lon = -71.33169049999999;
},
[1] Place {
name = 138 B St;
country = United States;
lat = 42.6285025;
lon = -71.3288776;
},
[2] Place {
name = 16 Bagley Ave;
country = United States;
lat = 42.6344084;
lon = -71.3378318;
},
[3] Place {
name = 27 Lane St;
country = United States;
lat = 42.6346452;
lon = -71.32436559999999;
},
[4] Place {
name = 39 Cosgrove St;
country = United States;
lat = 42.61964890000001;
lon = -71.3031683;
},
[5] Place {
name = 48 Houghton St;
country = United States;
lat = 42.6252232;
lon = -71.3243308;
},
[6] Place {
name = 49 Coral St;
country = United States;
lat = 42.6359846;
lon = -71.32655780000002;
},
[7] Place {
name = 59 Marriner St;
country = United States;
lat = 42.622121;
lon = -71.31365679999999;
}
)) <<<
我还有另一个数组,我在内存中维护,看起来像这样
print(distances)
["1.74", "1.45", "2.04", "1.50", "0.00", "1.15", "1.64", "0.56"]
结果
我的最终结果是这样的。
代码
这就是我渲染这张表的方式
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]
if indexPath.row == activePlace {
cell.address.isHidden = true
cell.distance.isHidden = true
}
}
return cell
}
我想要
显示我的结果按距离更近排序
注意:我的距离不是我的场所对象的一部分。我在运行时计算了它们。
如何去做这件事?
【问题讨论】:
-
使距离成为数据模型的一部分,用于填充表格。你只需要一个数组,而不是两个。
-
只对数组进行排序?
-
@rmaddy 每个点之间的距离是动态计算的,而不是存储在数据库中。我认为没有必要存储它。
-
它没有说存储距离。您需要一个用于表格视图的单独数据模型。使用您从数据库加载的数据和您在运行时计算的数据的组合来填充它。
-
我告诉过你在另一个问题上只使用一个数组:stackoverflow.com/questions/53174828/… 如您所见,如果您对
places进行排序,则需要以相同的方式对distances进行排序。此外,还有很多关于排序的问题,您没有显示任何尝试。
标签: ios swift uitableview tableview