I am using a popover tableview in my project. I want to change the tableview cell's text colour from grey to red on selection. And i also want the highlighted color to remain red when the popover tableview is loaded next time like left menu selection. Need help to do this. I have provided the code for the popover tableview.
我在我的项目中使用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 个解决方案
#1
1
I see a problem with your code relate to tableViewPopOver
. If you set selection style is .None
. You can't select cell.
我发现你的代码与tableViewPopOver有关。如果设置选择样式为.None。你不能选择细胞。
With your problem I can suggest for you two way to resolve:
有了你的问题,我可以建议你两种方式来解决:
-
If you want use
cell.textLabel?.highlightedTextColor
you will have face a problem: background of cell will be grey or blue depend on style selection you select. If you can accept it. You can implement like this:如果你想使用cell.textLabel?.highlightedTextColor你将面临一个问题:单元格的背景将是灰色或蓝色取决于你选择的样式选择。如果你能接受它。你可以像这样实现:
You create a variable to hold a cell selected. Maybe it is
int
value likevar indexSelected = 3
. And when you implementcellForRowAtIndexPath
you should implement like this:您创建一个变量来保持选定的单元格。也许它是像var indexSelected = 3这样的int值。当你实现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
And in
didSelectRowAtIndexPath
you updateindexSelected
:在didSelectRowAtIndexPath中更新indexSelected:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { indexSelected = indexPath.row //update selected row }
-
If you don't want cell background change color. You can choose this way. You should create variable like the way above. But in
cellForRowAtIndexPath
you should implement:如果您不希望单元格背景改变颜色。你可以选择这种方式。你应该像上面那样创建变量。但是在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! }
and in didSelectCellAtIndexPath
you should implement:
在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
}
Here Demo: Demo
这里演示:演示
Hope this help!
希望这有帮助!
#2
0
To change cell's text colour to red on selection you need to implemnet didSelectRowAtIndexPath
and change the cell textColour to red, but it will not stay red when you open it the next time. to handle that you need to have a member variable selectedIndexPath
. So it will be something like this
要在选择时将单元格的文本颜色更改为红色,您需要实现didSelectRowAtIndexPath并将单元格textColour更改为红色,但下次打开时它不会保持红色。要处理你需要有一个成员变量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
}
}
and to load the table next time with the selected cell red text colour you need to add this line in your cellForRowAtIndexPath
并使用所选单元格红色文本颜色下次加载表格,您需要在cellForRowAtIndexPath中添加此行
cell.
cell.textLabel?.highlightedTextColor = indexPath == selectedIndexPath ? UIColor.redColor() : UIColor.grayColor()
}
#1
1
I see a problem with your code relate to tableViewPopOver
. If you set selection style is .None
. You can't select cell.
我发现你的代码与tableViewPopOver有关。如果设置选择样式为.None。你不能选择细胞。
With your problem I can suggest for you two way to resolve:
有了你的问题,我可以建议你两种方式来解决:
-
If you want use
cell.textLabel?.highlightedTextColor
you will have face a problem: background of cell will be grey or blue depend on style selection you select. If you can accept it. You can implement like this:如果你想使用cell.textLabel?.highlightedTextColor你将面临一个问题:单元格的背景将是灰色或蓝色取决于你选择的样式选择。如果你能接受它。你可以像这样实现:
You create a variable to hold a cell selected. Maybe it is
int
value likevar indexSelected = 3
. And when you implementcellForRowAtIndexPath
you should implement like this:您创建一个变量来保持选定的单元格。也许它是像var indexSelected = 3这样的int值。当你实现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
And in
didSelectRowAtIndexPath
you updateindexSelected
:在didSelectRowAtIndexPath中更新indexSelected:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { indexSelected = indexPath.row //update selected row }
-
If you don't want cell background change color. You can choose this way. You should create variable like the way above. But in
cellForRowAtIndexPath
you should implement:如果您不希望单元格背景改变颜色。你可以选择这种方式。你应该像上面那样创建变量。但是在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! }
and in didSelectCellAtIndexPath
you should implement:
在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
}
Here Demo: Demo
这里演示:演示
Hope this help!
希望这有帮助!
#2
0
To change cell's text colour to red on selection you need to implemnet didSelectRowAtIndexPath
and change the cell textColour to red, but it will not stay red when you open it the next time. to handle that you need to have a member variable selectedIndexPath
. So it will be something like this
要在选择时将单元格的文本颜色更改为红色,您需要实现didSelectRowAtIndexPath并将单元格textColour更改为红色,但下次打开时它不会保持红色。要处理你需要有一个成员变量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
}
}
and to load the table next time with the selected cell red text colour you need to add this line in your cellForRowAtIndexPath
并使用所选单元格红色文本颜色下次加载表格,您需要在cellForRowAtIndexPath中添加此行
cell.
cell.textLabel?.highlightedTextColor = indexPath == selectedIndexPath ? UIColor.redColor() : UIColor.grayColor()
}