如何在着陆时选择细胞?

时间:2016-04-19 10:01:57

标签: ios uitableview

我有一个自定义的tablecell,并尝试在选中时显示不同的图标。问题是图标仅在 touchup 之后而不是在触地后立即更改(与选择背景图像不同)。有没有办法改变这种行为?

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    let cell = self
    if (selected) {
         cell.imageView?.image = UIImage(named: (selectedIconName!))

    } else {
         cell.imageView?.image = UIImage(named: (iconName!))

    }

3 个答案:

答案 0 :(得分:2)

您可以使用setHighlighted代替setSelected

override func setHighlighted(highlighted: Bool, animated: Bool) {
    super. setHighlighted(highlighted, animated: animated)

    let cell = self
    if (highlighted) {
         cell.imageView?.image = UIImage(named: (selectedIconName!))

    } else {
         cell.imageView?.image = UIImage(named: (iconName!))

    }
}

答案 1 :(得分:1)

覆盖自定义单元格子类中的touchesBegan

class Cell: UITableViewCell {
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        super.touchesBegan(touches, withEvent: event)
        // handle touch down
    }
}

答案 2 :(得分:0)

您最好拥有一个小型模型,array[NSIndexpath]然后在didSelectRow:上将indexPath添加到array并重新配置cell,然后传入选定的州。

on didSelectRow:

if selectedPaths.contains(indexPath) {
    let i = selectedPaths.indexOf(indexPath)
    selectedPaths.removeAtIndex(i)
} else {
    selectedPaths.append(indexPath)
}

configCell(indexPath)

在您的cellForRow中:您可以使用以下命令配置单元格:

 cell.configure(selected: selectedPaths.contains(indexPath))