Swift TableViewCell 问题:更改所选行的背景颜色
Issue with Swift TableViewCell: change background color for selected row
我的 tableView 有一个奇怪的问题。
我有一个音轨列表和一个音频播放器的 segue,以便在特定行播放 selected 音轨。一切正常!
我想更改 table 中 selected 行的背景颜色,这样,一旦用户播放音频并返回曲目列表(我的 Table View Controller) ,他可以看到哪些是以前 selected 行。
但是当我 运行 它不仅改变了我 select 编辑的索引路径行的颜色,还改变了索引路径 + 10 处的项目的颜色。
如果我 select 第一行,它会更改索引处行的颜色:0, 10, 20, 30...
为了改变 selected 单元格的颜色,我做了以下操作:
// MARK: - Navigation
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("audioPlayer", sender: tableView)
var selectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath) as CustomTableViewCell
selectedCell.contentView.backgroundColor = UIColor.greenColor()
selectedCell.backgroundColor = UIColor.blueColor()
请找到我的问题的屏幕截图,我只 select 编辑了三行:1、3、5,但我 select 编辑了 1、3、5、11、13、15 ,21,23 等等...:
https://www.dropbox.com/s/bhymu6q05l7tex7/problemaCelleColore.PNG?dl=0
有关详细信息 - 如果可以提供帮助 - 这是我的自定义 Table 查看 class:
import UIKit
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var artista: UILabel!
@IBOutlet weak var brano: UILabel!
var ascoltato = false
@IBOutlet weak var labelRiproduciAscoltato: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func setCell(artista: String, brano: String){
self.artista.text = artista
self.brano.text = brano
}
} // END MY CUSTOM TABLE VIEW CELL
这是我的方法 cellForRowAtIndexPath TableViewController:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath) as CustomTableViewCell
var tracks : Brani //Brani is my custom Object for handle my tracks
cell.setCell(tracks.title!, brano: tracks.author!)
return cell
}
我 运行正在 iPad 广播 iOS 7.1.
提前感谢您对我的问题提出任何建议或建议。
这可能是因为 UITableViewCells 被回收了。这意味着之前选择的 tableViewCell 被较低索引处的单元格重用。这是 UITableView 的预期行为并且是有道理的,因为它节省了内存使用量。要解决此问题,您需要让数据源跟踪选中的单元格,并相应地更新单元格的背景颜色。
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("audioPlayer", sender: tableView)
//datasource is updated with selected state
//cell is updated with color change
}
然后在你的 cellForRowAtIndexPath 方法中:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath) as CustomTableViewCell
var tracks : Brani //Brani is my custom Object for handle my tracks
cell.setCell(tracks.title!, brano: tracks.author!)
//update cell style here as well (by checking the datasource for selected or not).
return cell
}
我的 tableView 有一个奇怪的问题。 我有一个音轨列表和一个音频播放器的 segue,以便在特定行播放 selected 音轨。一切正常!
我想更改 table 中 selected 行的背景颜色,这样,一旦用户播放音频并返回曲目列表(我的 Table View Controller) ,他可以看到哪些是以前 selected 行。
但是当我 运行 它不仅改变了我 select 编辑的索引路径行的颜色,还改变了索引路径 + 10 处的项目的颜色。 如果我 select 第一行,它会更改索引处行的颜色:0, 10, 20, 30...
为了改变 selected 单元格的颜色,我做了以下操作:
// MARK: - Navigation
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("audioPlayer", sender: tableView)
var selectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath) as CustomTableViewCell
selectedCell.contentView.backgroundColor = UIColor.greenColor()
selectedCell.backgroundColor = UIColor.blueColor()
请找到我的问题的屏幕截图,我只 select 编辑了三行:1、3、5,但我 select 编辑了 1、3、5、11、13、15 ,21,23 等等...: https://www.dropbox.com/s/bhymu6q05l7tex7/problemaCelleColore.PNG?dl=0
有关详细信息 - 如果可以提供帮助 - 这是我的自定义 Table 查看 class:
import UIKit
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var artista: UILabel!
@IBOutlet weak var brano: UILabel!
var ascoltato = false
@IBOutlet weak var labelRiproduciAscoltato: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func setCell(artista: String, brano: String){
self.artista.text = artista
self.brano.text = brano
}
} // END MY CUSTOM TABLE VIEW CELL
这是我的方法 cellForRowAtIndexPath TableViewController:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath) as CustomTableViewCell
var tracks : Brani //Brani is my custom Object for handle my tracks
cell.setCell(tracks.title!, brano: tracks.author!)
return cell
}
我 运行正在 iPad 广播 iOS 7.1.
提前感谢您对我的问题提出任何建议或建议。
这可能是因为 UITableViewCells 被回收了。这意味着之前选择的 tableViewCell 被较低索引处的单元格重用。这是 UITableView 的预期行为并且是有道理的,因为它节省了内存使用量。要解决此问题,您需要让数据源跟踪选中的单元格,并相应地更新单元格的背景颜色。
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("audioPlayer", sender: tableView)
//datasource is updated with selected state
//cell is updated with color change
}
然后在你的 cellForRowAtIndexPath 方法中:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath) as CustomTableViewCell
var tracks : Brani //Brani is my custom Object for handle my tracks
cell.setCell(tracks.title!, brano: tracks.author!)
//update cell style here as well (by checking the datasource for selected or not).
return cell
}