gpt4 book ai didi

ios - 为什么展开 TableViewCell 时我的标签会移动,为什么点击时会出现不同的单元格动画?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:31:34 26 4
gpt4 key购买 nike

我制作了可扩展的 tableViewCell,它在点击时会展开和折叠,我的单元格中也有两个标签(dateLabeltextLabel)但是当我点击单元格,我的标签也随之移动。如何使它们静止并在点击时阻止它们移动。

这是我的类代码,其中包含 tableView:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

tableView.deselectRow(at: indexPath, animated: true)
let previousIndexPath = selectedIndexPath
if indexPath == selectedIndexPath {
selectedIndexPath = nil
} else {
selectedIndexPath = indexPath
}

var indexPaths : Array<IndexPath> = []
if let previous = previousIndexPath {
indexPaths += [previous]
}
if let current = selectedIndexPath {
indexPaths += [current]
}
if indexPaths.count > 0 {
tableView.reloadRows(at: indexPaths, with: UITableViewRowAnimation.automatic)
}

}

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

let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCellID") as! CustomCell
cell.textLabel?.text=list[indexPath.row]
cell.dateLabel?.text = time.indices.contains(indexPath.row) ? time[indexPath.row] : "Not Available"
print(time.indices.contains(indexPath.row))
return (cell)
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath == selectedIndexPath {
return CustomCell.expandedHeight
} else {
return CustomCell.defaultHeight
}
}

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
(cell as! CustomCell).watchFrameChanges()
}

override func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
(cell as! CustomCell).ignoreFrameChanges()
}


override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
for cell in tableView.visibleCells as! [CustomCell] {
cell.ignoreFrameChanges()
}
}

和我的 CustomCell 类:

var isObserving = false;

class var expandedHeight: CGFloat { get { return 170 } }
class var defaultHeight: CGFloat { get { return 40 } }

func checkHeight() {
textView.isHidden = (frame.size.height < CustomCell.expandedHeight)
}


func watchFrameChanges() {
if !isObserving {
addObserver(self, forKeyPath: "frame", options: [NSKeyValueObservingOptions.new, NSKeyValueObservingOptions.initial], context: nil)
isObserving = true;
}
}

func ignoreFrameChanges() {
if isObserving {
removeObserver(self, forKeyPath: "frame")
isObserving = false;
}
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "frame" {
checkHeight()
}
}

我还为不同的单元格获得不同的动画..就像我为第一个单元格获得幻灯片动画并为其余所有单元格获得淡入淡出

最佳答案

@PangHoMing I'm not using any constraints..on either of the labels. The dateLabel is on the stroyboard but the textLabel is called programmatically.

所以我认为这就是问题所在。例如,默认的 textLabel 带有始终水平居中的单元格。对于 dateLabel 情况,您需要在 Storyboard中设置约束。

否则 iOS 自动布局将以“他的”方式布局 View 。 https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/

快速建议:

避免使用默认单元格自带的cell.textLabel您可以通过在自定义单元格类中实现 layoutSubView 以编程方式布置单元格。但是,如果您不习惯使用代码来布局 UI,最好的方法是使用 Storyboard。

看看这些会有所帮助:

https://www.youtube.com/watch?v=EVJiprvRLoo

https://www.ralfebert.de/tutorials/ios-swift-uitableviewcontroller/custom-cells/

关于ios - 为什么展开 TableViewCell 时我的标签会移动,为什么点击时会出现不同的单元格动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42848066/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com