我在选择一行时显示图像,然后在选择另一行时使图像消失时遇到问题。如果我触摸选定的行,它不会被取消选择 - 这没关系,因为这是我想要的行为。我只想在触摸另一行时取消选择当前选定的行。
我用 Swift 编写。
我正在使用Kai Engelhardt's解决方案扩展所选行,如回答 here .
此 UIImage 应出现/消失:cellContent.ringImage.image = UIImage(named: "ring.png")
我猜我的逻辑在下面的selectedCellIndexPath部分是错误的。
这是我的代码:
在我的电视广告中:
class MenuViewController: UIViewController{
var selectedCellIndexPath: NSIndexPath?
let SelectedCellHeight: CGFloat = 222.0
let UnselectedCellHeight: CGFloat = 64.0
let menuItems = [
("1","test 1"),
("2","test 2"),
("3","test 3"),
("4","test 4"),
("5","test 5")]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == menuTable {
return menuItems.count
} else {
return 0}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MenuTableViewCell
if tableView == menuTable {
let (my, section) = menuItems[indexPath.row]
cell.myLabel.text = my
cell.sectionLabel.text = section
cell.selected = true
}
}
func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
if let selectedCellIndexPath = selectedCellIndexPath {
if selectedCellIndexPath == indexPath {
return SelectedCellHeight
}
}
return UnselectedCellHeight
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MenuTableViewCell
var cellContent = tableView.cellForRowAtIndexPath(indexPath) as! MenuTableViewCell
let cellLabel = cellContent.sectionLabel.text
if let selectedCellIndexPath = selectedCellIndexPath {
if selectedCellIndexPath != indexPath {
self.selectedCellIndexPath = indexPath
cellContent.ringImage.image = UIImage(named: "ring.png")
} else {
self.selectedCellIndexPath != indexPath
cellContent.ringImage.hidden = true
tableView.deselectRowAtIndexPath(indexPath, animated: true)
// cellContent.testbutton.removeFromSuperView
}
} else {
selectedCellIndexPath = indexPath
cellContent.ringImage.hidden = true
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
tableView.beginUpdates()
tableView.endUpdates()
}
非常感谢您的帮助!
谢谢
麦克
最佳答案
看来self.selectedCellIndexPath != indexPath
并没有达到你想要的取消选择标记效果。您可以尝试使用tableView.indexPathsForSelectedRows()
来获取当前选定的indexPath,将其与参数中的indexPath进行比较,然后在不分配self.selectedCellIndexPath
的情况下完成您的逻辑。
(已编辑) 我发现您还需要变量 self.selectedCellIndexPath 来识别高度,您可以尝试将其转换为计数器变量,该变量计算当前选定行的选定时间。如果它是奇数,则它被选中,而当它是偶数时,您知道您将取消选择它并将计数器变量重置为零。
func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
if (indexPath == tableView.indexPathsForSelectedRows()[0] && self.counter % 2 == 1) {
return SelectedCellHeight
}
return UnselectedCellHeight
}
关于ios - 如何在 SWIFT 的 UITableView 中选择行时添加图像,然后在取消选择行时将其删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30836361/