iOS where to put custom cell design? awakeFromNib or cellForRowAtIndexPath?
As you mentioned awakeFromNib is only called once (when the cell is instantiated), if its setting stuff like background colors and stuff like that that wont change then its ok to do it there, cell customization (the data that the cell is showing) should be done during cellForRowAtIndexPath, so you should not check if the data is empty there rather, give it the data everytime the cell is returned, this will allow you to reuse cells and set the data as needed
Hope this helps
Daniel
How to properly Init a custom UITableviewCell?
You can do your changes inside the DoneButtonCell
's class, either in the
- (void)awakeFromNib
{
.. essential to call super ..
super.awakeFromNib()
//Changes done directly here, we have an object
}
Or the initWithCoder:
method:
-(id)initWithCoder:(NSCoder*)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self)
{
//Changes here after init'ing self
}
return self;
}
UITableViewCell - Best place to set up the cell
You can use awakeFromNib
in your cell subclasses. This will be called when a new cell is created from the prototype in your storyboard, but not when a cell is re-used.
If you are using prototypes then the whole if (cell == nil) thing goes away, UITableView handles all that for you within the dequeue method.
Pass variable to custom UITableViewCell from UIViewController
You need to override prepareForReuse
in the cell. And remove it from tableView:indexPath:
. So when you scroll the cell is going to be reused but the isBotton
and isTop
vars will be reseted.
override func prepareForReuse() {
self.isBottom = false
self.isTop = false
}
Swift Custom UITableViewCell with programmatically created UITableView
awakeFromNib
won't get called unless the cell is loaded from a xib or storyboard. Move your custom code from awakeFromNib
into initWithStyle:reuseIdentifier:
because that's the initializer used by the tableView when you call its dequeueReusableCellWithIdentifier:
method
Custom UITableViewCell from nib in Swift
With Swift 5 and iOS 12.2, you should try the following code in order to solve your problem:
CustomCell.swift
import UIKit
class CustomCell: UITableViewCell {
// Link those IBOutlets with the UILabels in your .XIB file
@IBOutlet weak var middleLabel: UILabel!
@IBOutlet weak var leftLabel: UILabel!
@IBOutlet weak var rightLabel: UILabel!
}
TableViewController.swift
import UIKit
class TableViewController: UITableViewController {
let items = ["Item 1", "Item2", "Item3", "Item4"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "CustomCell")
}
// MARK: - UITableViewDataSource
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.middleLabel.text = items[indexPath.row]
cell.leftLabel.text = items[indexPath.row]
cell.rightLabel.text = items[indexPath.row]
return cell
}
}
The image below shows a set of constraints that work with the provided code without any constraints ambiguity message from Xcode:
Related Topics
Passing Values from One View Controller to Another in Swift
Atos Does Not Symbolicate System Frameworks/Libraries Properly
Get Apple Watch Heartratevariabilitysdnn Realtime
Wrong Text Height When Text Contains Emoji
How to Send Push Notification to Apns from iOS Device
Customize Google Maps Blue Dot for Current Location
Alamofire Request Gets Stuck When Entering Background
Ckfetchnotificationchangesoperation Sometimes Does Not Return Update, Delete Notifications
How to Programmatically Open Apple Watch Companion App from iOS App
Spritekit Not Respecting Zposition
How to Check Bitfields (Scnetworkreachabilityflags in Particular) for Flags in Swift
How to Make a Nsurlsesssion Get Request with Cookies
How to Mock Network Requests in Xcode UI Tests While The Tests Are Running
How to Record and Save at 240 Frames Per Second
Cocoapods Page Redirecting to Github
How to Get a Low Res Image, or Thumbnail from the Alassetrepresentation in Swift