我制作了可扩展的tableViewCell
,在点按时展开和折叠,我的单元格中还有两个标签(dateLabel
& textLabel
)但是当我点击单元格时,我的标签都会移动也有它。如何使它们静止并在敲击时阻止它们移动。
这是我的类代码,其中包含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()
}
}
我也为不同的细胞获得了不同的动画。就像我获得第一个细胞的幻灯片动画一样,为其余的细胞消失
答案 0 :(得分:0)
@PangHoMing我没有在任何一个标签上使用任何约束。 dateLabel位于stroyboard上,但调用了textLabel 编程。
所以我认为这就是问题所在。例如,默认textLabel
随单元格始终水平居中布局。对于dateLabel
情况,您需要在故事板中设置约束。
否则,iOS Autolayout会将视图布局在&#39;他的&#39;办法。 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/
答案 1 :(得分:0)
实际上这是你的布局问题。如果你想要你的标签是静态的,那么你只需要根据你的需要给出约束。如果你想要标签在同一位置,即使按下了Expand按钮也是如此然后给你的标签约束 - 领先的顶部高度宽度。它和它的框架将是相同的throghout。