Storing / retrieving master data in a user cell in Swift

The iOS application I write saves dates in Core Data, then calculates the Time between Today and Saved Date displayed in TableView.

I can successfully get the dates in Core Data and display the TimeBetween for each StoredDate in the TableView.

The problem occurs when I switched from standard cells to user cells. I'm not sure how to transfer the master data to each instance of Custom Cell. Or, if the underlying data is automatically passed to each user cell, I'm not sure how to access the variables.

Here's how I did it before switching to custom cells that worked:

// Generate the cells override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCellWithIdentifier("Tablecell", forIndexPath: indexPath) as UITableViewCell let countdownEntry = fetchedResultController.objectAtIndexPath(indexPath) as CountdownEntry // Grab the elements using the tag let labelCountdown:UILabel? = cell.viewWithTag(1) as UILabel? let labelName:UILabel? = cell.viewWithTag(2) as UILabel? let iconImage:UIImageView? = cell.viewWithTag(3) as UIImageView? labelCountdown.text = CountdownEngine.timeBetweenDatesRealTime(countdownEntry.date) 

In the user cell, I want to call the timeBetweenDatesRealTime function (which calculates the time between the stored date and today and displays the result in the label) after 1 second through the NSTimer function ( see here, how I set it if necessary ), but I don’t I can access countdownEntry.date .

Here is my own cell class:

 import UIKit class CountdownTableViewCell: UITableViewCell { // Outlets @IBOutlet weak var iconImage: UIImageView! @IBOutlet weak var labelName: UILabel! @IBOutlet weak var labelCountdown: UILabel! // Counter Variable var timeInterval: NSTimeInterval = 0 { didSet { labelCountdown.text = "\(timeInterval)" } } func updateUI() { println("updating custom cell") /* // Show real-time countdown of time remaining between today and saved date labelCountdown.text = CountdownEngine.timeBetweenDatesRealTime(countdownEntry.date) } */ } override func awakeFromNib() { super.awakeFromNib() // Initialization code println("code from cell called") // add listener let notificationCenter = NSNotificationCenter.defaultCenter() notificationCenter.addObserver(self, selector: Selector("updateUI"), name: "CustomCellUpdate", object: nil) } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } // MARK: self-cleanup deinit { NSNotificationCenter.defaultCenter().removeObserver(self) } } 
+1
source share
1 answer

If you are using a storyboard to customize cells, you need to change the cell class of the table view to your own class in the storyboard.

If you are not using a storyboard, you must register your own class of cells with registerClass:forCellReuseIdentifier:

Once you do this, the table view should return your own class so that you can change your code as follows: var cell = tableView.dequeueReusableCellWithIdentifier("Tablecell", forIndexPath: indexPath) as CountdownTableViewCell

And then you can create a property for countdownEntry in the CountdownTableViewCell class so that you can set this property in tableView:cellForRowAtIndex:

0
source

Source: https://habr.com/ru/post/982542/


All Articles