Swift:具有UITableView的单一选择

Swift:具有UITableView的单一选择,uitableview,swift,Uitableview,Swift,我有一个启用单一选择的常规UITableView。我的问题是,如果用户选择多行,则原始行仍保持选中状态。我还有一个问题,即无论我是否设置cell.selectionStyle=UITableViewCellSelectionStyle.Blue,高光都会保持灰色 我的视图控制器在情节提要中定义 表视图 内容:动态原型 选择:单选 触摸显示所选内容[X] 背景:黑色 索引行限制:0 表格单元格视图 款式:定制 精选:蓝色 背景:黑色 以下是一些屏幕截图: 这是我的密码: class Ar

我有一个启用单一选择的常规UITableView。我的问题是,如果用户选择多行,则原始行仍保持选中状态。我还有一个问题,即无论我是否设置cell.selectionStyle=UITableViewCellSelectionStyle.Blue,高光都会保持灰色

我的视图控制器在情节提要中定义

表视图

  • 内容:动态原型
  • 选择:单选
  • 触摸显示所选内容[X]
  • 背景:黑色
  • 索引行限制:0
表格单元格视图

  • 款式:定制
  • 精选:蓝色
  • 背景:黑色
以下是一些屏幕截图:

这是我的密码:

class AreaViewController: UITableViewController
{

    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.tableView.backgroundColor = backgroundColour
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCellWithIdentifier("areacell", forIndexPath: indexPath) as! UITableViewCell
        cell.selectionStyle = UITableViewCellSelectionStyle.Blue

        cell.textLabel?.text = "Cell Contents"
        cell.textLabel?.textColor = UIColor.whiteColor()

        return cell
    }

     override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
    {
         let cell = tableView.dequeueReusableCellWithIdentifier("areacell", forIndexPath: indexPath) as! UITableViewCell

    }
}
我肯定错过了一些明显的东西,但我没有看到任何不标准的东西。

UITableViewCellSelectionStyleBlue此单元格具有默认背景 选择时的颜色

在iOS 7中,选择颜色不再是蓝色。使用 改为UITableViewCellSelectionStyleDefault

如果要为选定单元格设置特殊背景色,则必须设置单元格的背景视图:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! UITableViewCell

    // Configure the cell...
    let backgroundView = UIView()
    backgroundView.backgroundColor = UIColor.redColor()
    cell.selectedBackgroundView = backgroundView

    return cell
}
看起来像这样:


啊!我终于找到了。好像我把let cell=tableView.dequeueReusableCellWithIdentifier(“areacell”,forIndexPath:indexPath)调用为!DidSelectRowatineXpath方法中的UITableViewCell。移除它会使一切重新开始工作。很明显。谢谢你的帮助,让我走上了正确的道路。

谢谢你。我以前尝试过这个代码,但它只起到了一定的作用。高亮显示是红色的,但只有当你一移开手指,行就被“触摸”时,它就会变灰,就像我原来的帖子一样。我不能责怪这一点,它是正常工作的。您是否在代码中的某个地方调用了
deselectRowAtIndexPath()
?或者您是无意中使用了
deselectRowAtIndexPath
而不是
didSelectRowAtIndexPath
?不久前,由于代码完成,我也遇到了同样的情况。区别很难看出:-)不,我已经检查了我的代码,在整个项目中我从来没有调用过Decelrowatindexpath。现在有人检查了我的神智,我将尝试从头开始重新创建视图。我会告诉你我的进展。谢谢