突出显示表格视图单元格 TextLabel 文本颜色
Posted
技术标签:
【中文标题】突出显示表格视图单元格 TextLabel 文本颜色【英文标题】:Highlight TableView Cell's TextLabel text colour 【发布时间】:2016-01-04 15:25:24 【问题描述】:我在我的项目中使用了弹出式表格视图。我想在选择时将 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)
【问题讨论】:
【参考方案1】:我发现您的代码存在与tableViewPopOver
相关的问题。如果设置选择样式为.None
。您不能选择单元格。
对于您的问题,我可以为您推荐两种解决方法:
如果您想使用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
如果您不希望单元格背景更改颜色。你可以选择这种方式。您应该像上面那样创建变量。但是在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!
在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
希望对您有所帮助!
【讨论】:
这不会改变任何事情 我觉得你的代码有问题,等我给你演示一下。 @GaneshKumar 检查我的编辑答案,演示我创建了两种方式,您只需取消注释即可。 我会检查并通知您 我的问题是当我选择一行时,popover tableview 将从视图中消失。当我再次打开它时,所选行应保持红色。这是编辑后的代码。【参考方案2】:要在选择时将单元格的文本颜色更改为红色,您需要实现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()
【讨论】:
我不懂你的技术 您想在选中时对单元格进行一些更改,对吗?您应该通过实现 tableViewDelegate 方法didSelectRowAtIndexPath
和 didDeselectRowAtIndexPath
来做到这一点。当您再次打开表格时,您还希望保留最后一个选定单元格的更改。然后你应该跟踪选择的indexPath并在cellForRowAtIndexPath
检查它如果你有任何问题,请不要犹豫
我还没有找到解决办法以上是关于突出显示表格视图单元格 TextLabel 文本颜色的主要内容,如果未能解决你的问题,请参考以下文章