ios - 如何在 SWIFT 的 UITableView 中选择行时添加图像,然后在取消选择行时将其删除

标签 ios swift uitableview ios8 uiimage

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

我用 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/

相关文章:

ios - 我应该为 Flutter 插件传入我的 Swift native 代码中的哪个 View Controller ?

ios - 从 iOS 应用程序调用网络服务

iphone - dyld : Library not loaded: @rpath/CoreNFC. 框架/CoreNFC、iOS11 和 Xcode 9 测试版

macos - 如何使用 Swift 1.2/Cocoa 以编程方式在 NSToolbar 上选择 NSToolbarItem

ios - 在 swift 4 中添加带计时器的导航栏刷新按钮

ios - MKAnnotationView 未从 MKMapView 中删除

ios - 使用 AVAudioPlayer 更改音高

ios - 无法快速使用计时器更改标签文本

Swift - 不同表格 View 单元格的通用模型

iphone - 从数组填充 UITableView