我在这个论坛四处逛逛,但没有发现与此相关的任何东西。
我想控制UITableView的选择功能:在选择和取消选择上设置单元格的样式,并使其在重用后保留。
用户点击动画时会执行的操作,当然会在 Dim user As WebClient = New WebClient()
Dim buffer As Byte() = user.DownloadData(filePath)
If Not buffer Is Nothing Then
Response.AddHeader("content-length", buffer.Length.ToString())
Response.AddHeader("Content-Disposition", "inline; filename=" + selectFile)
Response.ContentType = "application/pdf"
Response.BinaryWrite(buffer)
End If
的文件中取消:
TableViewController
但是..让我们假设您上下滚动,单元将被重用。我必须重新设置样式。我应该在哪里做?
A)?在func selectWorldMessage(indexPath: IndexPath) {
...
cell.attributedText = worldMessage.message.wholeWorldMessageAttributedString()
UIView.animate(withDuration: duration, animations: {
cell.bubbleImageView.tintColor = appColors.worldMessageBubbleSelected
cell.timeLabel.isHidden = false
cell.messageLabelBottomConstraint.constant = 14
cell.messageLabelTopConstraint.constant = 14
cell.timeLabel.alpha = 1.0
self.view.layoutIfNeeded()
self.tableView.beginUpdates()
self.tableView.endUpdates()
self.lastContentOffsetY = nil
}, completion: nil)
...
func deselectWorldMessage(indexPath: IndexPath) {
...
的{{1}}函数中?
TableViewController
B)还是在cellForRowAt
的{{1}}函数中?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
....
// If previously was selected
if (currentSelectedIndexPath == indexPath) {
cell.messageLabel.attributedText = worldMessage.message.wholeWorldMessageAttributedString()
cell.bubbleImageView.tintColor = appColors.worldMessageBubbleSelected
cell.timeLabel.isHidden = false
cell.messageLabelBottomConstraint.constant = 14
cell.messageLabelTopConstraint.constant = 14
} else {
cell.messageLabel.attributedText = worldMessage.message.shortenWorldMessageIfNeededAttributedString()
cell.bubbleImageView.tintColor = appColors.worldMessageBubble
cell.timeLabel.isHidden = true
cell.messageLabelBottomConstraint.constant = 10
cell.messageLabelTopConstraint.constant = 10
}
什么是更好的能源消耗呢?
答案 0 :(得分:1)
该代码应位于您的单元文件中,因为它与单元有关,并且在重用后仍将保留。它是由视图控制器处理单元格的“填充”逻辑,但是在这种情况下,您想更改单元格的UI样式。如果将代码放在视图控制器中,则在更改单元格时(通过添加或删除标签),也必须更改控制器。另外,您将需要保存选定的索引路径以检入cellForRowAt方法。
class TableViewCell: UITableViewCell {
override func setSelected(_ selected: Bool, animated: Bool) {
if selected {
self.backgroundColor = .red
} else {
self.backgroundColor = .green
}
}
}