简体   繁体   中英

Howto stop my cells image from changing every time it goes off screen?

I'm building a calendar app where free time in someone's day will be highlighted with a short message and one of three different images as a background for this cell.

What would be the best way to stop the image from changing every time the cell goes off screen? Should this logic be within the 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
}

Currently when you are styling the cell, you are using a random number to determine the background of the cell. Instead, pull this logic out of the cell and into the tableview. If you do that, then you can keep track of which image you are using and set the correct image to the cell to keep things consistent when scrolling around.

In your case, you can add another variable into the Timeblock class that sets the image number, or the image name, and use that to load the image in the cell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM