首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UItableView单元格第一行选择不起作用

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

Stack Overflow用户
提问于 2014-12-08 17:42:36
回答 2查看 1.9K关注 0票数 1

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

代码语言:javascript
复制
        // 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中,将颜色改为白色,如下所示。

代码语言:javascript
复制
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和delegate方法中,当我向下滚动并选择一行时,仍然得到相同的错误。

有什么建议吗?提前谢谢。

完成下面给出的代码。

代码语言:javascript
复制
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()    
    }

}
EN

回答 2

Stack Overflow用户

发布于 2014-12-08 17:52:15

您需要将选择数据保存在单独的结构中(为此,我推荐使用NSMutableSet ),并添加所选择的get行的indexPaths。

稍后在cellForRowAtIndexPath中,您需要检查indexPath是否在NSMutableSet中,并在此基础上配置一个单元backgroundColor。选中索引路径后,您将被迫重新加载索引路径中的表行。

你有问题的行为的原因是UITableViewCell在不同的indexPaths上被重用,并且它准备重用(重置选择)。

票数 0
EN

Stack Overflow用户

发布于 2014-12-08 18:59:28

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

代码语言:javascript
复制
 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()
            }
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27355191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档