Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何在用户选择或取消选择自定义UITableViewCell时更改其高度?_Swift_Uitableview - Fatal编程技术网

Swift 如何在用户选择或取消选择自定义UITableViewCell时更改其高度?

Swift 如何在用户选择或取消选择自定义UITableViewCell时更改其高度?,swift,uitableview,Swift,Uitableview,在我的项目中,我有一个UITableView,每次用户点击任何单元格时,单元格的高度都会随着单元格内的一些长文本而增加,当再次点击它或点击另一个单元格时,单元格的高度会降低并显示一些短文本。(带有一些动画) 这是我的自定义单元格代码: class SomeCustomCell:UITableViewCell{ 静态let identifier=“SomeCustomCellId” var shortDescLabel:AvatarLabel! var longDescLabel:AvatarLa

在我的项目中,我有一个
UITableView
,每次用户点击任何单元格时,单元格的高度都会随着单元格内的一些长文本而增加,当再次点击它或点击另一个单元格时,单元格的高度会降低并显示一些短文本。(带有一些动画)

这是我的自定义单元格代码:

class SomeCustomCell:UITableViewCell{
静态let identifier=“SomeCustomCellId”
var shortDescLabel:AvatarLabel!
var longDescLabel:AvatarLabel!
覆盖功能设置选定(uSelected:Bool,动画:Bool){
super.setSelected(选定,动画:动画)
backgroundColor=所选?UIColor.浅灰色.带AlphaComponent(0.5):.clear
如果选择{
已选定()
}否则{
取消选择()
}
}
重写init(样式:UITableViewCell.CellStyle,reuseIdentifier:String?){
init(样式:style,reuseIdentifier:reuseIdentifier)
selectionStyle=.none
shortDescLabel=AvatarLabel()
shortDescLabel.translatesAutoresizingMaskIntoConstraints=false
addSubview(shortDescLabel)
NSLayoutConstraint.activate([
shortDescLabel.leadingAnchor.constraint(等式:leadingAnchor,常数:10),
shortDescLabel.topAnchor.constraint(等式:topAnchor,常数:20),
shortDescLabel.heightAnchor.constraint(相等常量:50),
shortDescLabel.trailingAnchor.constraint(等式:trailingAnchor,常数:-10)
])
longdesclab=AvatarLabel()
longDescLabel.alpha=0.0
longDescLabel.numberOfLines=0
longdateLabel.ishiden=true
longDescLabel.translatesAutoresizingMaskIntoConstraints=false
addSubview(longDescLabel)
NSLayoutConstraint.activate([
longDescLabel.leadingAnchor.constraint(等式:leadingAnchor,常数:10),
longDescLabel.topAnchor.constraint(等式:topAnchor,常数:20),
longDescLabel.heightAnchor.constraint(equalToConstant:100),
longDescLabel.trailingAnchor.constraint(等式:trailingAnchor,常数:-10)
])
}
必需初始化?(编码器:NSCoder){
fatalError(“初始化(编码者:)尚未实现”)
}
func didSelected(){
longDescLabel.ishiden=false
shortDescLabel.ishiden=true
UIView.animate(持续时间:0.5){[弱自我]在
self?.shortDescLabel.alpha=0.0
self?.longdesclab.alpha=1.0
}
}
func取消选择(){
longDescLabel.ishiden=true
shortDescLabel.ishiden=false
背景颜色=.clear
UIView.animate(持续时间:0.5){[弱自我]在
self?.shortDescLabel.alpha=1.0
self?.longdesclab.alpha=0.0
}
}
func setSomeCustomCell(someModel:someModel){
shortDescLabel.text=someModel.shortText
longdesclab.text=someModel.longText
}
}
这是我的
UITableView
代码:

扩展名someViewController:UITableViewDelegate,UITableViewDataSource{ func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{ 如果someModels.count==0{ tableView.setEmptyView() }否则{ tableView.restore() } 返回someModels.count } func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{ guard let cell=tableView.dequeueReusableCell(标识符为:SomeCustomCell.identifier)为?SomeCustomCell-else{ 返回SomeCustomCell() } 让item=someModels[indexPath.row] cell.setSomeCustomCell(项目) 返回单元 } func tableView(tableView:UITableView,didSelectRowAt indexPath:indexPath){ 如果选择了CellIndeXPath==indexPath{ self.selectedCellIndexPath=nil guard let cell=tableView.cellForRow(at:indexPath)作为?SomeCustomCell-else{return} 单元格。取消选择() }否则{ self.selectedCellIndexPath=indexPath } tableView.beginUpdate() tableView.endUpdates() 如果选择了CellIndexPath!=nil{ //这样可以确保单元格在展开后完全可见 scrollToRow(at:indepath,at:.none,动画:true) } } func tableView(tableView:UITableView,heightForRowAt indexath:indexPath)->CGFloat{ 如果选择了CellIndeXPath==indexPath{ 返回140 } 返回90 } }
但有一个问题,当用户点击某个单元格并再次点击它(单元格先变大,然后变小)并向下滚动(所选单元格现在不再显示在屏幕上),然后向上滚动以查看该单元格时,单元格的高度很小,但显示的是长数据(而不是短数据)。为什么会发生这种情况以及如何解决此问题?

调用您自己的func来更改单元格的“选定/未选定”外观不会将此更改通知表视图

使用当前代码,当您再次点击某个单元格时,您会告诉该单元格更改其外观,但据表视图所知,该单元格仍处于选中状态

因此,当您向下滚动然后向上滚动时,表格视图会自动“重新选择”该行

将您的
didSelectRowAt
更改为:func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if selectedCellIndexPath == indexPath { self.selectedCellIndexPath = nil // tell the table view to deselect the row! tableView.deselectRow(at: indexPath, animated: true) //guard let cell = tableView.cellForRow(at: indexPath) as? SomeCustomCell else { return } //cell.deSelected() } else { self.selectedCellIndexPath = indexPath } tableView.beginUpdates() tableView.endUpdates() if selectedCellIndexPath != nil { // This ensures, that the cell is fully visible once expanded tableView.scrollToRow(at: indexPath, at: .none, animated: true) } }