Loading a Reusable UITableViewCell from a Nib
Just implement a method with the appropriate method signature:
- (NSString *) reuseIdentifier {
return @"myIdentifier";
}
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:
Nib-file loaded UIView in UITableViewCell does not stretch
I solved it by also adding a width constraint matching the tableViews width. This is code from CustomTableViewCell:
public override func layoutSubviews() {
super.layoutSubviews()
if let width = tableView()?.frame.width, !haveAddedWidthConstraint {
haveAddedWidthConstraint = true
rowView.addWidthConstraint(width: width)
}
}
UIViewExtension:
public func addWidthConstraint(width: CGFloat) {
let widthConstraint = NSLayoutConstraint(item: self, attribute: .width, relatedBy: .greaterThanOrEqual, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: width)
widthConstraint.priority = 1000
addConstraint(widthConstraint)
}
UITableViewCellExtension:
func tableView() -> UITableView? {
var currentView: UIView = self
while let superView = currentView.superview {
if superView is UITableView {
return (superView as! UITableView)
}
currentView = superView
}
return nil
}
Cannot load XIB into reusable TableViewCell
While I was missing to add my messagesTable
's dataSource which was what Sh_Khan recommended, the solution for my error afterwards was that my reference outlets were pointing to "File's owner" rather than my File's outlets.
So, when selecting the custom TableViewCell
(not the prototype) should be:
For some reason, instead of being "Title Label" on the right side, it was "File's owner"
And the prototype cell shouldn't contain any class attached to it.
Failed to Load Custom Cell from dequeue reusable UITableViewCell
There are two methods named dequeueReusableCell
.
dequeueReusableCell(withIdentifier:)
dequeueReusableCell(withIdentifier:for:)
But the unwrapping fails and the app crashes. tableView.dequeueReusableCell returns nil for some reason....
You are using the first one and the doc clearly says
Return Value
A
UITableViewCell
object with the associatedidentifier
ornil
if no such object exists in the reusable-cell queue.
You may want to use the latter.
Change the line:
let cell = tableView.dequeueReusableCell(withIdentifier: "Custom") as! CustomCell
To:
let cell = tableView.dequeueReusableCell(withIdentifier: "Custom", for: indexPath) as! CustomCell
Related Topics
Xcode 4.5 and iOS 4.2.1 Incompatibility
iPad Remembering Camera Permissions After Delete-How to Clear
Class Amsupporturlconnectiondelegate Is Implemented in Both
Counting the Number of Lines in a Uitextview, Lines Wrapped by Frame Size
Is Silent Remote Notifications Possible If User Has Disabled Push for the App
Programmatically Creating an Expanding Uitableviewcell
Uitableview - Change Section Header Color
Uiscrollview: Paging Horizontally, Scrolling Vertically
In iOS, How to Store a Secret "Key" That Will Allow Me to Communicate with My Server
How to Use Any in Codable Type
Uicollectionview: Must Be Initialized with a Non-Nil Layout Parameter
Detecting Collisions in Sprite Kit
How to Achieve a "Clock Wipe"/ Radial Wipe Effect in iOS
Combine Framework: How to Process Each Element of Array Asynchronously Before Proceeding