如何阻止我的细胞图像每次离开屏幕时都发生变化?
Howto stop my cells image from changing every time it goes off screen?
我正在构建一个日历应用程序,其中将用一条短消息和三个不同图像之一作为此单元格的背景突出显示某人一天中的空闲时间。
阻止每次单元格离开屏幕时图像发生变化的最佳方法是什么?
这个逻辑应该在 UITableViewCell 中吗?
class FreeTimeCell: UITableViewCell {
@IBOutlet weak var background: UIImageView!
@IBOutlet weak var freeTimeMessage: UILabel!
@IBOutlet weak var freeTimePeriod: UILabel!
@IBOutlet weak var freeTime: UILabel!
var timeblock: Timeblock! {
didSet {
self.freeTimePeriod.text = "\(timeblock.startTime.dateToString()) - \(timeblock.endTime.dateToString())"
self.freeTime.text = "OccupiedTime.FreeTime".localized
self.freeTimeMessage.text = MessagesInteractor.getFreetimeTimeblock()
let randomNumber = arc4random_uniform(3)
let imageName = "freetime\(randomNumber)"
self.background.image = UIImage(named: imageName)
}
}
override func awakeFromNib() {
super.awakeFromNib()
//removes highlighting of the cell when tapping on it
self.selectionStyle = UITableViewCellSelectionStyle.none
}
TableViewController
/**
Checks which data cell type belongs to this cell, and returns correct table cell accordingly
*/
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch self.dataCells[indexPath.row].cellType {
case .ConflictHeader, .TimeToFirstMeetingHeader :
return self.generateHeaderCell(indexPath: indexPath)
case .FreeTime:
return self.generateFreeTimeCell(indexPath: indexPath)
case .SingleMeeting:
return self.generateSingleMeetingCell(dataCell: self.dataCells[indexPath.row], indexPath: indexPath)
}
}
/**
Generates Free Time Cell (between meetings to show user free spot)
- parameter indexPath: used to dequeue the reusable cell
- returns: Free time cell generated
*/
private func generateFreeTimeCell(indexPath: IndexPath) -> FreeTimeCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "FreeTimeCell", for: indexPath) as! FreeTimeCell
cell.timeblock = self.dataCells[indexPath.row].timeblock
return cell
}
目前,在设置单元格样式时,您使用的是随机数来确定单元格的背景。相反,将此逻辑从单元格中拉出并放入表视图中。如果这样做,那么您可以跟踪正在使用的图像并将正确的图像设置到单元格以在滚动时保持一致。
对于您的情况,您可以将另一个变量添加到设置图像编号或图像名称的时间块中 class,并使用它在单元格中加载图像。
我正在构建一个日历应用程序,其中将用一条短消息和三个不同图像之一作为此单元格的背景突出显示某人一天中的空闲时间。
阻止每次单元格离开屏幕时图像发生变化的最佳方法是什么? 这个逻辑应该在 UITableViewCell 中吗?
class FreeTimeCell: UITableViewCell {
@IBOutlet weak var background: UIImageView!
@IBOutlet weak var freeTimeMessage: UILabel!
@IBOutlet weak var freeTimePeriod: UILabel!
@IBOutlet weak var freeTime: UILabel!
var timeblock: Timeblock! {
didSet {
self.freeTimePeriod.text = "\(timeblock.startTime.dateToString()) - \(timeblock.endTime.dateToString())"
self.freeTime.text = "OccupiedTime.FreeTime".localized
self.freeTimeMessage.text = MessagesInteractor.getFreetimeTimeblock()
let randomNumber = arc4random_uniform(3)
let imageName = "freetime\(randomNumber)"
self.background.image = UIImage(named: imageName)
}
}
override func awakeFromNib() {
super.awakeFromNib()
//removes highlighting of the cell when tapping on it
self.selectionStyle = UITableViewCellSelectionStyle.none
}
TableViewController
/**
Checks which data cell type belongs to this cell, and returns correct table cell accordingly
*/
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch self.dataCells[indexPath.row].cellType {
case .ConflictHeader, .TimeToFirstMeetingHeader :
return self.generateHeaderCell(indexPath: indexPath)
case .FreeTime:
return self.generateFreeTimeCell(indexPath: indexPath)
case .SingleMeeting:
return self.generateSingleMeetingCell(dataCell: self.dataCells[indexPath.row], indexPath: indexPath)
}
}
/**
Generates Free Time Cell (between meetings to show user free spot)
- parameter indexPath: used to dequeue the reusable cell
- returns: Free time cell generated
*/
private func generateFreeTimeCell(indexPath: IndexPath) -> FreeTimeCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "FreeTimeCell", for: indexPath) as! FreeTimeCell
cell.timeblock = self.dataCells[indexPath.row].timeblock
return cell
}
目前,在设置单元格样式时,您使用的是随机数来确定单元格的背景。相反,将此逻辑从单元格中拉出并放入表视图中。如果这样做,那么您可以跟踪正在使用的图像并将正确的图像设置到单元格以在滚动时保持一致。
对于您的情况,您可以将另一个变量添加到设置图像编号或图像名称的时间块中 class,并使用它在单元格中加载图像。