UItableView单元格第一行选择不起作用

时间:2014-12-08 09:42:36

标签: ios uitableview swift

我有UITableView,其中我将第一行的背景颜色设置为灰色。通过此代码

        // showing the selection in UITableViewCell

        if indexPath.row == 0
        {
           cell.backgroundColor = UIColor(red: 189 / 255, green: 189 / 255, blue: 189 / 255, alpha: 0.55)
        }

现在我将代码放在didSelectItemAtIndexPath中,将颜色更改为白色。

var cell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))

// hiding the seleciton in UITableViewCell

if indexPath.row == 0
{
   cell?.backgroundColor = UIColor.whiteColor()
}

当表格加载时,它会在第一行显示灰色背景,表示选择了第一行,并且值显示在DetailViewController中。如果我单击其他行,则第0行颜色将变为白色,而其他行将由UITableSelectionStyleGrey变为灰色。

表加载后,如果向下滚动很长。如果我连续选择,那么突然它应用程序崩溃并显示Fatal-Error : Unexpectedly found nil while unwrapping a nil value。我不知道为什么会发生这种情况。我尝试将这些简单的代码放在dataSource和委托方法中,当我向下滚动并选择一行时仍然会出现相同的错误。

有任何建议吗?提前谢谢。

完整的代码如下。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as WordsCustomTableViewCell

    cell.selectionStyle = UITableViewCellSelectionStyle.Gray

    // showing the selection in UITableViewCell

    if indexPath.row == 0
    {
        cell.backgroundColor = UIColor(red: 189 / 255, green: 189 / 255, blue: 189 / 255, alpha: 0.55)
    }

    return cell
}

// MARK: - UITableViewDelegate Methods

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{

    var cell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))

   // hiding the selection in UITableViewCell

    if indexPath.row == 0
    {
       cell?.backgroundColor = UIColor.whiteColor()    
    }

}

2 个答案:

答案 0 :(得分:0)

您需要将选择数据保留在单独的结构中(我建议使用NSMutableSet)并添加已选择的行的indexPath。

稍后在cellForRowAtIndexPath中,您需要检查indexPath是否在NSMutableSet内,并根据该配置单元格backgroundColor。在被选中后,您将被迫在索引路径重新加载表格行。

您遇到问题的行为原因是UITableViewCell在不同的indexPath之间重复使用,并且它已准备好重复使用(重置选择)。

答案 1 :(得分:0)

如果我正确理解你的问题,试试这可能对你有帮助。

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
        {
    //        var cell:UITableViewCell = self.tableObj.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
    //        cell.textLabel?.text="I am learning swift Day \(indexPath.row)"

             var cell:customTableViewCell = self.tableObj.dequeueReusableCellWithIdentifier("customCell") as customTableViewCell
            cell.nameLbl.text=nameList[indexPath.row]
            cell.imageView?.image=imgListArray[indexPath.row] as? UIImage

            if indexPath.row == 0
            {
                cell.backgroundColor = UIColor(red: 189 / 255, green: 189 / 255, blue: 189 / 255, alpha: 0.55)
            }
            cell.selectionStyle = UITableViewCellSelectionStyle.Gray


            return cell
        }
        func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
        {

            var cell = self.tableObj.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))

            // hiding the selection in UITableViewCell

            if indexPath.row != 0
            {
                cell?.backgroundColor = UIColor.whiteColor()
            }
        }
相关问题