【问题标题】:How to add an image when a row is selected and then remove it when it is deselected in UITableView in SWIFT如何在选择行时添加图像,然后在 SWIFT 的 UITableView 中取消选择时将其删除
【发布时间】:2015-06-15 01:45:16
【问题描述】:

我遇到了一个问题,即在选择一行时显示图像,然后在选择另一行时使图像消失。如果我触摸选定的行,它不会被取消选择——这没关系,因为这是我想要的行为。我只希望在触摸另一行时取消选择当前选定的行。

我正在用 Swift 写作。

我正在使用Kai Engelhardt's 解决方案来扩展选定的行,正如here 所回答的那样。

这个 UIImage 应该出现/消失:cellContent.ringImage.image = UIImage(named: "ring.png")

我猜我的逻辑在下面的 selectedCellIndexPath 部分是错误的。

这是我的代码:

在我的 TVC 中:

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()       
}

非常感谢您的帮助!

谢谢

迈克

【问题讨论】:

    标签: ios swift uitableview ios8 uiimage


    【解决方案1】:

    看来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
    }
    

    【讨论】:

    • 感谢您的建议。当我用您的代码替换 heightForRowAtIndexPath 中的 SelectedCellIndexPath 代码时: if (indexPath == tableView.indexPathsForSelectedRows()[0] && self.counter % 2 == 1) { 我收到错误“AnyObject 无法转换为 NSObject”,当我垂头丧气! NSObject 我收到一个错误“找不到接受提供的参数的 '==' 的重载”。如果我在 indexPathsForSelectedRows()[0] 末尾删除 [0] 它会编译但不会扩展行大小。
    • 我还删除了 selectedCellIndexPath 部分并将其替换为: if tableView.indexPathsForSelectedRows() != indexPath { cellContent.ringImage.image = UIImage(named: "ring.png") } else { cellContent .ringImage.removeFromSuperview() } 这给出了与当前相同的效果。
    • (tableView.indexPathsForSelectedRows() as! [NSIndexPaths])[0] 然后比较 indexPath.row 如果是单选tableView呢? tableView.indexPathsForSelectedRows() 将返回一个 Objects 数组,你应该在这里向下转型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    相关资源
    最近更新 更多