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

时间:2018-11-06 15:19:50

标签: ios swift uitableview realm tableview

我有一个列表,其中包含从 Realm 数据库中查询的 places 的4个对象。

theme.js

我要隐藏所选的内容。

例如当我单击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; } )) 时,我不希望它显示在列表中。

enter image description here

在Swift 4中如何做到这一点?


代码

Daniel Webster Highway

2 个答案:

答案 0 :(得分:1)

您可以将所选行的索引从placeVC传递到PlaceDetailVC以及

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

    if indexPath.row == passedIndex {
        return 0
    }

    return 70
}

将单元格高度设置为0以隐藏该单元格。

答案 1 :(得分:1)

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,其余图像的高度为44pt,等等)。

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

NB:代码未经测试,可能无法编译,但是您应该明白这一点。

相关问题