UITableView with custom cells is empty - ios

I have a two custom cells implemented:
class TextFieldCell: UITableViewCell{
var label = UILabel()
var textField = UITextField()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
label.text = "Name"
textField.placeholder = "Enter Task Name Here"
addSubview(label)
addSubview(textField)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
class SwitchCell : UITableViewCell {
var label = UILabel()
var switchControl = UISwitch()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
label.text = "State"
switchControl.on = true
addSubview(label)
addSubview(switchControl)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
In my table view class i have declared this two cells:
let textFieldCell = TextFieldCell(style: .Default, reuseIdentifier: "textFieldCell")
let switchCell = SwitchCell(style: .Default, reuseIdentifier: "switchCell")
and have delegate method in it:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
switch indexPath.row {
case 0: return textFieldCell
case 1: return switchCell
default: return UITableViewCell()
}
}
But when table view loads i've see only switch control on the left of second row. I don't know why.

Try this:
private let reuseIdentifier = "switchCell"
//identifier of your cell.
Seems you have missed registering the cell.
let tableNib = UINib(nibName:"SwitchCell", bundle:nil)
self.tableView!.registerNib(tableNib,forCellReuseIdentifier:reuseIdentifier)

TextFieldCell Added below Code in ViewDidLoad of TableViewCell
self.tableView.registerClass(SwitchCell, forCellReuseIdentifier: "switchCell")
self.tableView.registerClass(TextFieldCell, forCellReuseIdentifier: "textFieldCell")

Related

how to resolve DiffableDataSource mulipleCell crash (iOS 13)

I encountered an error during development.
The minimum target is iOS 13.
You want to give different cells for each section using DiffableDataSource.
I don't know why there's an error.
I need your help.
minumum target: iOS 13.0 *
🚨 errorCode 🚨
Thread 1: "unable to dequeue a cell with identifier MuseumItemCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard"
don't used Storyboard, only programming UI
-- ✅ CODE ✅ --
import UIKit
import SnapKit
final class MuseumViewController: UIViewController {
enum Item: Hashable {
case bannerItem([String])
case numberItem([Int])
}
enum MySection: Int {
case banner
case number
}
var tableView = UITableView()
var dataSource: UITableViewDiffableDataSource<MySection, Item>!
var banners: [String] {
[
"ABCDEFGHIJKL",
"ABCDE",
"A",
]
}
var numbers: [Int] = [100000, 10000, 1000, 100, 10, 10]
override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
fetchDataSource()
fetchSnapShot()
}
func setupTableView() {
view.addSubview(tableView)
tableView.register(MuseumBannerCell.self, forCellReuseIdentifier: MuseumBannerCell.identifier)
tableView.register(MuseumNumberCell.self, forCellReuseIdentifier: MuseumNumberCell.identifier)
tableView.snp.makeConstraints {
$0.edges.equalToSuperview()
}
}
func fetchSnapShot() {
var snapshot = NSDiffableDataSourceSnapshot<MySection, Item>()
snapshot.appendSections([.banner, .number])
snapshot.appendItems([.bannerItem(banners), .numberItem(numbers)])
dataSource.apply(snapshot)
}
func fetchDataSource() {
dataSource = UITableViewDiffableDataSource<MySection, Item>(
tableView: tableView
) { tableView, indexPath, itemIdentifier in
let section = MySection(rawValue: indexPath.section)
switch section {
case .banner:
guard let cell = tableView.dequeueReusableCell(
withIdentifier: MuseumBannerCell.id,
for: indexPath
) as? MuseumBannerCell else { return UITableViewCell() }
cell.backgroundColor = .red
return cell
case .number:
guard let cell = tableView.dequeueReusableCell(
withIdentifier: MuseumNumberCell.id,
for: indexPath
) as? MuseumNumberCell else { return UITableViewCell() }
cell.backgroundColor = .blue
return cell
default:
return UITableViewCell()
}
}
}
}
class MuseumBannerCell: UITableViewCell {
static let id: String = "MuseumBannerCell"
// MARK: - Initialize
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class MuseumNumberCell: UITableViewCell {
static let id: String = "MuseumItemCell"
// MARK: - Initialize
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
In your setupTableView method you set
tableView.register(MuseumBannerCell.self, forCellReuseIdentifier: MuseumBannerCell.identifier)
tableView.register(MuseumNumberCell.self, forCellReuseIdentifier: MuseumNumberCell.identifier)
and later you call
tableView.dequeueReusableCell(withIdentifier: MuseumBannerCell.id, for: indexPath)
Try to register the cells with MuseumBannerCell.id (and MuseumNumberCell.id) since that's the same identifier you use for dequeuing.
tableView.register(MuseumBannerCell.self, forCellReuseIdentifier: MuseumBannerCell.id)
tableView.register(MuseumNumberCell.self, forCellReuseIdentifier: MuseumNumberCell.id)

Programmatic UITableView is showing one cell only

A Swift newbie here. I'm trying to learn how to create different UI elements programmatically. I've hit the following wall..
I have 2 .swift files, on one hand we have...
import UIKit
struct MyTableView {
let myCustomTable: UITableView = {
let aTable = UITableView()
aTable.register(MyCustomCell.self, forCellReuseIdentifier: "myCell")
return aTable
}()
}
// custom cell class, then add subviews (UI controls) to it
class MyCustomCell: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubview(aLabel)
aLabel.frame = CGRect(x:0,
y:0,
width:self.frame.width,
height:self.frame.height)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
// stuff to add to the cell
let aLabel: UILabel = {
let lbl = UILabel()
lbl.text = "My Custom Cell"
lbl.backgroundColor = UIColor.yellow // just to highlight it on the screen
return lbl
}()
On the other hand, we have the following view controller...
import UIKit
class ViewControllerA: UIViewController, UITableViewDelegate, UITableViewDataSource {
private let instanceOfViewControllerTable = MyTableView()
override func loadView() {
super.loadView()
view.frame = UIScreen.main.bounds
instanceOfViewControllerTable.myCustomTable.delegate = self
instanceOfViewControllerTable.myCustomTable.dataSource = self
instanceOfViewControllerTable.myCustomTable.frame = CGRect(x:0,
y:0,
width:self.view.frame.width,
height:self.view.frame.height)
self.view.addSubview(instanceOfViewControllerTable.myCustomTable)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
}
}
It builds and runs successfully, however, I'm getting the following result:
Now, my thinking is if I'm doing something wrong, the cell shouldn't appear at all. What I don't understand, why is it showing only on one cell in the array?
Your help is highly appreciated.
You are declaring aLabel as a global variable. That way, only one instance exists from it. Move it inside your cell's class declaration.
class MyCustomCell: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubview(aLabel)
aLabel.frame = CGRect(x:0,
y:0,
width:self.frame.width,
height:self.frame.height)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let aLabel: UILabel = {
let lbl = UILabel()
lbl.text = "My Custom Cell"
lbl.backgroundColor = UIColor.yellow // just to highlight it on the screen
return lbl
}()
}

Swift. Proper initialization of UITableViewCell hierarchy

[UITableViewCell] <- [genericCell] <- [Cell1], [Cell2], [Cell3]
Hello. Please imagine hierarchy above. In my code I don't have objects exactly of type genericCell, but this class shares some properties.
What design of inits should be in my code? I have following structure for genericCell:
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
//my stuff (initializing shared properties)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
But what about Cell1? How can I invoke init(style: UITableViewCellStyle, reuseIdentifier: String?) in genericCell for "my stuff" operations through initialisation of Cell1 instance? Now they doesn't perform.
EDIT
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let typeOfCell = FbDataManager.sharedInstance.posts[indexPath.row][FbDataManager.sharedInstance.typeParameter] as! String
switch typeOfCell {
case self.linkTypeOfPost:
var cell = tableView.dequeueReusableCellWithIdentifier(self.linkCellIdentifier) as? FbLinkPostViewCell
if cell == nil {
cell = FbLinkPostViewCell.init(style: .Default, reuseIdentifier: self.linkCellIdentifier)
}
//...
Hi again. This is part from tableView's delegate, btw I copy-pasted Abhinav's inits to my code and again those inits aren't working. (no output to console)
I'm not sure I understand your question correctly, but it seems to be about inheritance between classes. So basically you have a "GenericCell" class that inherits from "UITableViewCell", and "CellOne", "CellTwo", and "CellThree" classes that each inherit from "GenericCell". If you want to go through init with style, one way to set this up would be like this:
class GenericCell: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
// code common to all your cells goes here
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
class CellOne: GenericCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier) // the common code is executed in this super call
// code unique to CellOne goes here
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
You could then create instances of CellOne in your table view's data source like so:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell")
if (cell == nil) {
cell = CellOne.init(style: .Default, reuseIdentifier: "cell")
}
return cell!
}
For each instance it will now first go through the common setup done in "GenericCell", and then through the unique setup in "CellOne". "CellTwo" and "CellThree" would be set up accordingly.
EDIT
A more concrete example of how to configure instances of all three Cell types:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// you need to write a method like this to figure out which type you need:
let cellID = self.cellIDForIndexPath(indexPath) // returns either "cell1", "cell2" or "cell3"
// dequeue or init a cell of the approriate type
var cell = tableView.dequeueReusableCellWithIdentifier(cellID)
if (cell == nil) {
switch cellID {
case "cell1": cell = CellOne.init(style: .Default, reuseIdentifier: "cell")
case "cell2": cell = CellTwo.init(style: .Default, reuseIdentifier: "cell")
case "cell3": cell = CellThree.init(style: .Default, reuseIdentifier: "cell")
default: cell = UITableViewCell()
}
}
// configure the individual cell if needed (you need to implement methods + logic here that fit your data)
(cell as! GenericCell).configureForData(self.dataForIndexPath(indexPath))
return cell!
}
This is how I would lay down the hierarchy mentioned by you:
Step 1 : Make Generic Cell class
class GenericCell : UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
print("Generic Cell Initialization Done")
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
Step 2 : Make Specific Cell 1 class:
class MyCell1 : GenericCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
print("MyCell1 Initialization Done")
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
Step 3 : Make Specific Cell 2 class:
class MyCell2 : GenericCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
print("MyCell2 Initialization Done")
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
Step 4 : Make Specific Cell 3 class:
class MyCell3 : GenericCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
print("MyCell3 Initialization Done")
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
Step 5 : Finally use the cells like this:
let cell1 = MyCell1.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell1")
let cell2 = MyCell2.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell2")
let cell3 = MyCell3.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell3")
PS: This would guarantee setting the properties in generic cell as well in specific cells.
EDIT: This is how you would use cells in cellForRowAtIndexPath:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell1 = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as MyCell1
if cell1 == nil {
cell1 = MyCell1.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell1")
}
// Do your cell property setting
return cell1
} else if indexPath.section == 1 {
let cell2 = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as MyCell2
if cell2 == nil {
cell2 = MyCell2.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell2")
}
// Do your cell property setting
return cell2
} else {
let cell3 = tableView.dequeueReusableCellWithIdentifier("cell3", forIndexPath: indexPath) as MyCell3
if cell3 == nil {
cell3 = MyCell3.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell3")
}
// Do your cell property setting
return cell3
}
}

Custom TableViewCell without storyboard

I am using custom cocoa class extends from TableViewCell and it doesn't give any error message but the cells do not appear in the tableview. The scroll gets longer but the table cells are not viewable.
I typed this in my ViewController :
tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "Cell")
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)->UITableViewCell
{
var cell:CustomTableViewCell? = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as? CustomTableViewCell
if cell == nil {
cell = CustomTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
}
cell!.labTime.text = filtered[indexPath.row].name!
return cell!
}
and my cell class looks like
var labTime = UILabel()
override func awakeFromNib() {
// Initialization code
super.awakeFromNib()
labTime = UILabel(frame: contentView.bounds)
labTime.font = UIFont(name:"HelveticaNeue-Bold", size: 16)
contentView.addSubview(labTime)
}
I don't use any storyBoard or xib file.
Can not find the solution,
thanks
Do this way.
All view intialization of properties should go in init method.
Add this in your custom cell class
var labTime: UILabel!
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
//Do your cell set up
labTime = UILabel(frame: contentView.bounds)
labTime.font = UIFont(name:"HelveticaNeue-Bold", size: 16)
contentView.addSubview(labTime)
}
Add the below line in viewDidLoad method of your view controller.
tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "Cell")
Set these two delegate - UITableViewDataSource and UITableViewDelegate

Assigning property of custom UITableViewCell

I am using the below custom UITableViewCell without nib file.
I have no problem in viewing it in my UITableViewController.
My problem is with assigning a text to "name" label cell.name.text = "a Name" .. noting is assigned
Can you help me?
import UIKit
class ACMenuCell: UITableViewCell {
#IBOutlet var name : UILabel!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
name = UILabel(frame: CGRectMake(20, 10, self.bounds.size.width , 25))
name.backgroundColor = .whiteColor()
self.contentView.addSubview(name)
self.contentView.backgroundColor = .blueColor()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func layoutSubviews() {
super.layoutSubviews()
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
You might want to try this: Create a public function in a subclass of UITableViewCell, and inside this funxtion set the text to label.
(void)initializeCell
{
do whatever like setting the text for label which is a subview of tableViewCell
}
And from cellForRowAtIndexPAth, call this function on cell object:
// taking your code for demo
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let reuseIdentifierAC:NSString = "ACMenuCell"; var cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifierAC, forIndexPath:indexPath) as ACMenuCell cell = ACMenuCell(style: UITableViewCellStyle.Default, reuseIdentifier: reuseIdentifierAC) [cell initializeCell] return cell }
This approach has worked for me.

Resources