突出显示TableView Cell的TextLabel文本颜色

时间:2016-01-04 15:25:24

标签: ios swift uitableview

我在项目中使用popover tableview。我想在选择时将tableview单元格的文本颜色从灰色更改为红色。并且我还希望下次加载popover tableview时突出显示的颜色保持红色,如左菜单选择。需要帮助才能做到这一点。我提供了popover tableview的代码。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if tableView === tableViewPopOver {
        let cell = UITableViewCell(style: .Default, reuseIdentifier: nil)
        cell.textLabel?.text = FeedFilter.allValues[indexPath.row].rawValue
      if indexSelected == indexPath.row {
        cell.textLabel?.textColor = UIColor.redColor()
      } else {
        cell.textLabel?.textColor = UIColor.lightGrayColor()
      }

        //cell.selectionStyle = .None
        return cell
    }


    let cell = tableView.dequeueReusableCellWithIdentifier(kDreamFeedTableViewCell, forIndexPath: indexPath) as! DreamFeedTableViewCell
    cell.selectionStyle = .None

    let dream = self.arrayDreams[indexPath.row]
    cell.dream = dream
    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if tableView === tableViewPopOver {
        //tableViewPopOver?.cellForRowAtIndexPath(indexPath)?.textLabel?.highlightedTextColor = UIColor.redColor()
        //selectedIndexPath = indexPath.row

        let cell = tableView.cellForRowAtIndexPath(NSIndexPath.init(forRow: indexSelected, inSection: 0))
        cell?.textLabel?.textColor = UIColor.lightGrayColor()
        let cell2 = tableView.cellForRowAtIndexPath(indexPath)
        cell2?.textLabel?.textColor = UIColor.redColor()
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        indexSelected = indexPath.row

        self.popover.dismiss()
        NRLoader.showLoader()
        self.searchDreams(true)
    }

    else { // dream feed tableview

        tableView .deselectRowAtIndexPath(indexPath, animated: false)
        let dream = self.arrayDreams[indexPath.row]

        if !isValidUser(dream.user) {
            return
        }

        if isCurrentUser(dream.user)
        {
            self.pushToUserDreamViewControllerForDream(dream)
        }
        else
        {
            tableView .deselectRowAtIndexPath(indexPath, animated: false)
            self.pushToFeedDetailViewControllerForDream(dream)
        }

    }
}

2 个答案:

答案 0 :(得分:1)

我发现您的代码与tableViewPopOver有关。如果您设置选择样式为.None。你不能选择细胞。

有了你的问题,我可以建议你两种方法解决:

  1. 如果您想使用cell.textLabel?.highlightedTextColor,您将遇到问题:单元格的背景将为灰色或蓝色,具体取决于您选择的样式选择。如果你能接受它。您可以这样实现:

    您创建一个变量来保持选定的单元格。也许它是int的值var indexSelected = 3。当你实现cellForRowAtIndexPath时,你应该这样实现:

    cell.textLabel?.text = FeedFilter.allValues[indexPath.row].rawValue
    cell.textLabel?.highlightedTextColor = UIColor.redColor()
    if indexPath.row == indexSelected {
        tableView.selectRowAtIndexPath(indexPath, animated:true, scrollPosition: .None)
    }
    return cell
    

    didSelectRowAtIndexPath更新indexSelected

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    
        indexSelected = indexPath.row //update selected row
    }
    
  2. 如果您不希望细胞背景改变颜色。你可以选择这种方式。你应该像上面那样创建变量。但是在cellForRowAtIndexPath中你应该实现:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell")//init cell it depend on your way create cell
        cell?.textLabel?.text = "Your text"
    
        if indexSelected == indexPath.row {
            cell?.textLabel?.textColor = UIColor.redColor()
        } else {
            cell?.textLabel?.textColor = UIColor.lightGrayColor()
        }
    
    
       return cell!
    }
    
  3. 并在didSelectCellAtIndexPath中实施:

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let cell = tableView.cellForRowAtIndexPath(NSIndexPath.init(forRow: indexSelected, inSection: 0))
        cell?.textLabel?.textColor = UIColor.lightGrayColor()
        let cell2 = tableView.cellForRowAtIndexPath(indexPath)
        cell2?.textLabel?.textColor = UIColor.redColor()
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        indexSelected = indexPath.row //update selected row
    }
    

    此处演示:Demo

    希望这有帮助!

答案 1 :(得分:0)

要在选择时将单元格的文本颜色更改为红色,您需要实现didSelectRowAtIndexPath并将单元格文本颜色更改为红色,但下次打开时它不会保持红色。要处理你需要一个成员变量selectedIndexPath。所以它会是这样的

var selectedIndexPath:NSIndexPath!
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if let cell = tableView.cellForRowAtIndexPath(indexPath) {
        cell.textLabel?.highlightedTextColor = UIColor.redColor()
        selectedIndexPath = indexPath
    }
}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    if let cell = tableView.cellForRowAtIndexPath(indexPath) {
        cell.textLabel?.highlightedTextColor = UIColor.grayColor()
        selectedIndexPath = nil
    }
}

要使用选定的单元格红色文本颜色下次加载表格,您需要在cellForRowAtIndexPath

中添加此行

<强>细胞

cell.textLabel?.highlightedTextColor = indexPath == selectedIndexPath ?  UIColor.redColor() : UIColor.grayColor()
 }
相关问题