API Call is not reloading on TableView (with MVVM) - swift

I'm building an app that works with GEODb Rapid API. It should decode country names and put them in TableView Cells.
Working with MVVM pattern and getting the result of api in my modelView.
My modelView looks as below:
class HomePageViewModel {
var color = UIColor.white
var apiResult: APIResult?
init() {
getAPIResult()
}
private func getAPIResult() {
APIHandler.urlRequest { results in
print(results)
self.apiResult = results
}
}
}
I do get the data. Printing the result is shown as below on terminal.
2021-11-27 19:01:07.687230+0300 Countries[32016:2991279] [boringssl] boringssl_metrics_log_metric_block_invoke(144) Failed to log metrics
APIResult(data: [Countries.APIResultKeys(name: "Vatican City"), Countries.APIResultKeys(name: "Ethiopia"), Countries.APIResultKeys(name: "Montenegro"), Countries.APIResultKeys(name: "Namibia"), Countries.APIResultKeys(name: "Ghana")])
2021-11-27 19:03:28.937963+0300 Countries[32016:2991
I'm aware of boringss1 issue but will handle it later for now.
My main class is as below. I am initializing the class with the viewModel. So I'm getting the data from the main model.
class HomePageViewController: UIViewController {
deinit {
print("HomePageViewController Deinit")
}
//MARK: - Computed Properties
private lazy var countriesTableView: UITableView = {
let temp = UITableView()
temp.translatesAutoresizingMaskIntoConstraints = false
temp.delegate = self
temp.dataSource = self
temp.register(CountriesTableViewCell.self, forCellReuseIdentifier: CountriesTableViewCell.identifier) //Registering Cell
temp.separatorStyle = .none // Removing lines out of tableView
return temp
}()
private var viewModel: HomePageViewModel?
//MARK: - LifeCycle
init(with viewModel: HomePageViewModel) {
self.viewModel = viewModel
super.init(nibName: nil, bundle: nil)
setupViews()
setupConstraintsForViews()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
I'm trying to change the text label of the cell inside my main class and via the UITableView stubs as below. But the labels are not changing. I tried to reload the table view in cellForRowAt function but when I do that my table view just disappears.
So what am I doing wrong?
Excuse me if I'm not explaining properly.
extension HomePageViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: CountriesTableViewCell.identifier, for: indexPath) as? CountriesTableViewCell else { return UITableViewCell() }
cell.countryLabel.text = viewModel?.apiResult?.data[indexPath.row].name
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 60
}

Related

Is this ok when implementing multiple table views in Swift5?

I'm implementing two table views on a single view controller in Swift 5. I realise this can be done with a single extension to the view controller by identifying the table views by name. However, I'd like to keep the code separate, and so I've done it as shown in this simple example here - this has involved looking at several examples on the web and so a general thank you to all who post these things! The main one is the answer by Girish Ghoda here:
Two tables on one view in swift
It all seems to work, but I'm wondering if I'm breaking any important rules...
There are two table views on the view controller, with simple constraints and outlets tableView1 and tableView2.
This is the ViewController.swift file
import UIKit
var array1 = ["one", "two", "three"]
var array2 = ["left", "right", "centre", "outside"]
class ViewController: UIViewController {
#IBOutlet weak var tableView1: UITableView!
#IBOutlet weak var tableView2: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
initTableViews()
}
var dataSource1: DataSource1!
var dataSource2: DataSource2!
func initTableViews() {
dataSource1 = DataSource1()
tableView1.dataSource = dataSource1
tableView1.delegate = dataSource1
dataSource2 = DataSource2()
tableView2.dataSource = dataSource2
tableView2.delegate = dataSource2
}
}
There are then two files:
TableViewClass1.swift
import UIKit
class DataSource1: NSObject, UITableViewDataSource, UITableViewDelegate {
override init(){
super.init()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array1.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath)
cell.textLabel?.numberOfLines = 0
cell.textLabel?.text = array1[indexPath.row]
return cell
}
}
TableViewClass2.swift:
import UIKit
class DataSource2: NSObject, UITableViewDataSource, UITableViewDelegate {
override init(){
super.init()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array2.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath)
cell.textLabel?.numberOfLines = 0
cell.textLabel?.text = array2[indexPath.row]
return cell
}
}
As I say, everything seems ok, so it may seem strange to ask about it, but while the approach seems to give simple code structure, I'm wondering if there is anything here that could lead to problems.
Many thanks,
Ian
It's fine, but if both classes are that redundant I would use one DataSource class with a convenience initializer and lazy instantiated dataSource properties
class DataSource: NSObject, UITableViewDataSource, UITableViewDelegate {
var array : [String]
let cellIdentifier : String
init(array: [String], cellIdentifier : String )
{
self.array = array
self.cellIdentifier = cellIdentifier
super.init()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
cell.textLabel?.numberOfLines = 0
cell.textLabel?.text = array[indexPath.row]
return cell
}
}
lazy var dataSource1: DataSource = {
return DataSource(array: array1, cellIdentifier: "cell1")
}()
lazy var dataSource2: DataSource = {
return DataSource(array: array2, cellIdentifier: "cell2")
}()
func initTableViews() {
tableView1.dataSource = dataSource1
tableView1.delegate = dataSource1
tableView2.dataSource = dataSource2
tableView2.delegate = dataSource2
}

I want to position top UIView on top of UITableView using SnapKit

I'm written in snapkit for UI render. I have not use storyboard or nib files.
I want to position top UIView on top of UITableView using SnapKit.
How to write it?
Below is my written code
swift
import UIKit
import SnapKit
class ViewController: UIViewController {
var data: [String] = []
var tableView = UITableView()
var subView = UIView()
var label = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
// Do any additional setup after loading the view, typically from a nib.
label.text = "Hello?"
self.view.addSubview(self.tableView)
self.tableView.addSubview(self.subView)
self.subView.addSubview(self.label)
self.subView.backgroundColor = .gray
self.tableView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
self.subView.snp.makeConstraints { make in
make.top.width.equalTo(self.tableView)
make.centerX.equalTo(self.tableView)
make.height.equalTo(200)
}
self.label.snp.makeConstraints { make in
make.centerX.centerY.equalTo(self.subView)
}
for i in 0...100 {
data.append("\(i)")
}
self.tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let element = self.data[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "comicListCell") ?? UITableViewCell(style: .normal, reuseIdentifier: "comicListCell")
cell.textLabel?.text = element
return cell
}
}
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(self.data[indexPath.row])
}
}
My code :
I want it!:
UIView must placed into UITableView children !!
Sorry my bad english..
Thanks for reading
You want subview which is sticky header of tableview. Change this line self.tableView.addSubview(self.subView) to
tableView.tableHeaderView = subView.
You could easily add the tableView method:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
return self.subView
} else {
return UIView.init(frame: CGRect.zero)
}
}

UITableView Custom Class not showing results

I have a ViewController on the storyboard.
TestViewController.swift
class TestViewController: UIViewController {
var users : Dictionary = [String:User]()
let customTableView : UITableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
customTableView.register(UITableViewCell.self, forCellReuseIdentifier: "userCell")
let customTable = CustomTable(users:users)
customTableView.delegate = customTable
customTableView.dataSource = customTable
view.addSubview(customTableView)
customTableView.translatesAutoresizingMaskIntoConstraints = false
customTableView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
customTableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
customTableView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
customTableView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
}
}
CustomTable.swift
class CustomTable: NSObject, UITableViewDelegate, UITableViewDataSource {
var users : Dictionary = [String:User]()
var userNames = ["Steve", "Joe", "Bob"]
var userIds = [String]()
init(users: [String:User]) {
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("HEY")
return userNames.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("HIHI")
let cell = tableView.dequeueReusableCell(withIdentifier: "userCell", for: indexPath)
let text = userNames[indexPath.row]
cell.textLabel?.text = text
return cell
}
}
numberOfRowsInSection - seems to fire
cellForRowAt - doesn't fire / print "HIHI", therefore, my table is empty.
I have moved creating the table to viewDidAppear, nothing.
I have searched tirelessly to find similar situations, but nothing seems to solve it. Does anything stick out to anyone or have done this successfully?
Declare customTable reference at class level. It becomes nil as soon as it leaves the scope of viewDidLoad method.

UITAbleview Cell For Row at Indexpath Not Called

For some reason, the tableview reloadData function is being called despite the fact that cellForRowAtIndexPath function is not being called. I have posted the full code below. In a nutshell, using the videos as the data source, each time the values of videos changes, the table reloads its data. I have confirmed that this part of functionality works as I have debugged it and the number of videos outputs each time that function occurs. Despite this, cellForRowAtIndexPath fails to be called even once.
import Foundation
import UIKit
import ReactiveCocoa
import enum Result.NoError
public typealias NoError = Result.NoError
public class VideosTable: UITableView, UITableViewDataSource, UITableViewDelegate {
// MARK: Public variables
public var currentVideo: Video!
// MARK: Private variables
private let videos = MutableProperty<[Video]?>(nil)
private let cellId = "cell"
// MARK Initializers
public required init(signal: Signal<[Video]?, NoError>, frame: CGRect) {
super.init(frame: frame, style: .Plain)
self.dataSource = self
self.delegate = self
self.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellId)
videos <~ signal
videos.signal.observeNext{_ in
self.reloadData()
}
}
videos <~ signal
videos.signal.observeNext{_ in
print("values changed")
self.reloadData()
}
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
// MARK: DataSource
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let videoCount = videos.value?.count ?? 0
print("Video Count: \(videoCount)")
return videoCount
}
public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.dequeueReusableCellWithIdentifier(cellId) ?? UITableViewCell()
cell.textLabel?.text = videos.value![indexPath.row].title
return cell
}
public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 60.0
}
// MARK: Delegate
public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("User selected row at \(indexPath.row)")
currentVideo = videos.value![indexPath.row]
}
}
Problem has been solved. The code above is actually all correct, it was in another part of the application that was causing the error. Basically the table wasn't being correctly added to the view hierarchy and thus CellForRow was not being called while tableReload was. Likewise, the height of the table was actually zero upon initialization and remained zero. If the height of each TableCell, which is determined by the callback, heightForRowAtIndexPath exceeds that of the table height, CellForRow will never be called.

creating custom tableview cells in swift

I have a custom cell class with a couple of IBOutlets. I have added the class to the storyboard. I have connected all my outlets. my cellForRowAtIndexPath function looks like this:
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as SwipeableCell
cell.mainTextLabel.text = self.venueService.mainCategoriesArray()[indexPath.row]
return cell
}
Here is my custom cell class:
class SwipeableCell: UITableViewCell {
#IBOutlet var option1: UIButton
#IBOutlet var option2: UIButton
#IBOutlet var topLayerView : UIView
#IBOutlet var mainTextLabel : UILabel
#IBOutlet var categoryIcon : UIImageView
init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
}
When I run the app, all my cell are empty. I have logged out self.venueService.mainCategoriesArray() and it contains all the correct strings. I have also tried putting an actual string equal to the label, and that produces the same result.
What am I missing? Any help is appreciated.
Custom Table View Cell Example
Tested with Xcode 9 (edit also tested on 11 / 12 Beta 2) and Swift 4 (edit: also tested on 5.2)
The asker of the original question has solved their problem. I am adding this answer as a mini self contained example project for others who are trying to do the same thing.
The finished project should look like this:
Create a new project
It can be just a Single View Application.
Add the code
Add a new Swift file to your project. Name it MyCustomCell.swift. This class will hold the outlets for the views that you add to your cell in the storyboard.
import UIKit
class MyCustomCell: UITableViewCell {
#IBOutlet weak var myView: UIView!
#IBOutlet weak var myCellLabel: UILabel!
}
We will connect these outlets later.
Open ViewController.swift and make sure you have the following content:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
// These strings will be the data for the table view cells
let animals: [String] = ["Horse", "Cow", "Camel", "Sheep", "Goat"]
// These are the colors of the square views in our table view cells.
// In a real project you might use UIImages.
let colors = [UIColor.blue, UIColor.yellow, UIColor.magenta, UIColor.red, UIColor.brown]
// Don't forget to enter this in IB also
let cellReuseIdentifier = "cell"
#IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
// number of rows in table view
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.animals.count
}
// create a cell for each table view row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:MyCustomCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! MyCustomCell
cell.myView.backgroundColor = self.colors[indexPath.row]
cell.myCellLabel.text = self.animals[indexPath.row]
return cell
}
// method to run when table view cell is tapped
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You tapped cell number \(indexPath.row).")
}
}
Setup the storyboard
Add a Table View to your view controller and use auto layout to pin it to the four sides of the View Controller. Then drag a Table View Cell onto the Table View. And then drag a View and a Label onto the Prototype cell. (You may need to select the Table View Cell and manually set the Row Height to something taller in the Size inspector so that you have more room to work with.) Use auto layout to fix the View and the Label how you want them arranged within the content view of the Table View Cell. For example, I made my View be 100x100.
Other IB settings
Custom class name and Identifier
Select the Table View Cell and set the custom class to be MyCustomCell (the name of the class in the Swift file we added). Also set the Identifier to be cell (the same string that we used for the cellReuseIdentifier in the code above.
Hook Up the Outlets
Control drag from the Table View in the storyboard to the tableView variable in the ViewController code.
Do the same for the View and the Label in your Prototype cell to the myView and myCellLabel variables in the MyCustomCell class.
Finished
That's it. You should be able to run your project now.
Notes
The colored views that I used here could be replaced with anything. An obvious example would be a UIImageView.
If you are just trying to get a TableView to work, see this even more basic example.
If you need a Table View with variable cell heights, see this example.
This is for who are working custom cell with .xib
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let identifier = "Custom"
var cell: CustomCell! = tableView.dequeueReusableCellWithIdentifier(identifier) as? CustomCel
if cell == nil {
tableView.registerNib(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: identifier)
cell =tableView.dequeueReusableCellWithIdentifier(identifier) as? CustomCell
}return cell}
I have the same problem.
Generally what I did is the same as you.
class dynamicCell: UITableViewCell {
#IBOutlet var testLabel : UILabel
init(style: UITableViewCellStyle, reuseIdentifier: String) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
and in the uitableviewcell method:
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell :dynamicCell = tableView.dequeueReusableCellWithIdentifier("cell") as dynamicCell
cell.testLabel.text = "so sad"
println(cell.testLabel)
return cell;
}
and yeah the tableview shows nothing! But guess what, it actually shows something...because the log I get from the println(cell.testLabel) shows that all the Labels are actually displayed out.
BUT! their Frames is strange, which have something like this:
frame = (0 -21; 42 21);
so it has a (0,-21) as (x,y), so that means the label just appears at somewhere outside the bound of the cell.
so I try to add adjust the frame manually like this:
cell.testLabel.frame = CGRectMake(10, 10, 42, 21)
and sadly, it doesn't work.
---------------update after 10 min -----------------
I DID IT.
so, it seems that the problem comes from the Size Classes.
Click on your .storyboard file and go to the File Inspector Tab
UNCHECK THE Size Classes checkbox
and finally, my "so sad"Label comes out!
Thanks for all the different suggestions, but I finally figured it out. The custom class was set up correctly. All I needed to do, was in the storyboard where I choose the custom class: remove it, and select it again. It doesn't make much sense, but that ended up working for me.
Last Updated Version is with xCode 6.1
class StampInfoTableViewCell: UITableViewCell{
#IBOutlet weak var stampDate: UILabel!
#IBOutlet weak var numberText: UILabel!
override init?(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init(coder aDecoder: NSCoder) {
//fatalError("init(coder:) has not been implemented")
super.init(coder: aDecoder)
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
Details
Xcode Version 10.2.1 (10E1001), Swift 5
Solution
import UIKit
// MARK: - IdentifiableCell protocol will generate cell identifier based on the class name
protocol Identifiable: class {}
extension Identifiable { static var identifier: String { return "\(self)"} }
// MARK: - Functions which will use a cell class (conforming Identifiable protocol) to `dequeueReusableCell`
extension UITableView {
typealias IdentifiableCell = UITableViewCell & Identifiable
func register<T: IdentifiableCell>(class: T.Type) { register(T.self, forCellReuseIdentifier: T.identifier) }
func register(classes: [Identifiable.Type]) { classes.forEach { register($0.self, forCellReuseIdentifier: $0.identifier) } }
func dequeueReusableCell<T: IdentifiableCell>(aClass: T.Type, initital closure: ((T) -> Void)?) -> UITableViewCell {
guard let cell = dequeueReusableCell(withIdentifier: T.identifier) as? T else { return UITableViewCell() }
closure?(cell)
return cell
}
func dequeueReusableCell<T: IdentifiableCell>(aClass: T.Type, for indexPath: IndexPath, initital closure: ((T) -> Void)?) -> UITableViewCell {
guard let cell = dequeueReusableCell(withIdentifier: T.identifier, for: indexPath) as? T else { return UITableViewCell() }
closure?(cell)
return cell
}
}
extension Array where Element == UITableViewCell.Type {
var onlyIdentifiables: [Identifiable.Type] { return compactMap { $0 as? Identifiable.Type } }
}
Usage
// Define cells classes
class TableViewCell1: UITableViewCell, Identifiable { /*....*/ }
class TableViewCell2: TableViewCell1 { /*....*/ }
// .....
// Register cells
tableView.register(classes: [TableViewCell1.self, TableViewCell2.self]. onlyIdentifiables)
// Create/Reuse cells
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (indexPath.row % 2) == 0 {
return tableView.dequeueReusableCell(aClass: TableViewCell1.self, for: indexPath) { cell in
// ....
}
} else {
return tableView.dequeueReusableCell(aClass: TableViewCell2.self, for: indexPath) { cell in
// ...
}
}
}
Full Sample
Do not forget to add the solution code here
import UIKit
class ViewController: UIViewController {
private weak var tableView: UITableView?
override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
}
}
// MARK: - Setup(init) subviews
extension ViewController {
private func setupTableView() {
let tableView = UITableView()
view.addSubview(tableView)
self.tableView = tableView
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
tableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
tableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
tableView.register(classes: [TableViewCell1.self, TableViewCell2.self, TableViewCell3.self].onlyIdentifiables)
tableView.dataSource = self
}
}
// MARK: - UITableViewDataSource
extension ViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int { return 1 }
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 20 }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch (indexPath.row % 3) {
case 0:
return tableView.dequeueReusableCell(aClass: TableViewCell1.self, for: indexPath) { cell in
cell.textLabel?.text = "\(cell.classForCoder)"
}
case 1:
return tableView.dequeueReusableCell(aClass: TableViewCell2.self, for: indexPath) { cell in
cell.textLabel?.text = "\(cell.classForCoder)"
}
default:
return tableView.dequeueReusableCell(aClass: TableViewCell3.self, for: indexPath) { cell in
cell.textLabel?.text = "\(cell.classForCoder)"
}
}
}
}
Results
Uncheck "Size Classes" checkbox works for me as well, but you could also add the missing constraints in the interface builder. Just use the built-in function if you don't want to add the constraints on your own. Using constraints is - in my opinion - the better way because the layout is independent from the device (iPhone or iPad).
It is Purely swift notation an working for me
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cellIdentifier:String = "CustomFields"
var cell:CustomCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? CustomCell
if (cell == nil)
{
var nib:Array = NSBundle.mainBundle().loadNibNamed("CustomCell", owner: self, options: nil)
cell = nib[0] as? CustomCell
}
return cell!
}
[1] First Design your tableview cell in StoryBoard.
[2] Put below table view delegate method
//MARK: - Tableview Delegate Methods
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return <“Your Array”>
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
var totalHeight : CGFloat = <cell name>.<label name>.frame.origin.y
totalHeight += UpdateRowHeight(<cell name>.<label name>, textToAdd: <your array>[indexPath.row])
return totalHeight
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell : <cell name>! = tableView.dequeueReusableCellWithIdentifier(“<cell identifier>”, forIndexPath: indexPath) as! CCell_VideoCall
if(cell == nil)
{
cell = NSBundle.mainBundle().loadNibNamed("<cell identifier>", owner: self, options: nil)[0] as! <cell name>;
}
<cell name>.<label name>.text = <your array>[indexPath.row] as? String
return cell as <cell name>
}
//MARK: - Custom Methods
func UpdateRowHeight ( ViewToAdd : UILabel , textToAdd : AnyObject ) -> CGFloat{
var actualHeight : CGFloat = ViewToAdd.frame.size.height
if let strName : String? = (textToAdd as? String)
where !strName!.isEmpty
{
actualHeight = heightForView1(strName!, font: ViewToAdd.font, width: ViewToAdd.frame.size.width, DesignTimeHeight: actualHeight )
}
return actualHeight
}
Set tag for imageview and label in cell
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return self.tableData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("imagedataCell", forIndexPath: indexPath) as! UITableViewCell
let rowData = self.tableData[indexPath.row] as! NSDictionary
let urlString = rowData["artworkUrl60"] as? String
// Create an NSURL instance from the String URL we get from the API
let imgURL = NSURL(string: urlString!)
// Get the formatted price string for display in the subtitle
let formattedPrice = rowData["formattedPrice"] as? String
// Download an NSData representation of the image at the URL
let imgData = NSData(contentsOfURL: imgURL!)
(cell.contentView.viewWithTag(1) as! UIImageView).image = UIImage(data: imgData!)
(cell.contentView.viewWithTag(2) as! UILabel).text = rowData["trackName"] as? String
return cell
}
OR
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "imagedataCell")
if let rowData: NSDictionary = self.tableData[indexPath.row] as? NSDictionary,
urlString = rowData["artworkUrl60"] as? String,
imgURL = NSURL(string: urlString),
formattedPrice = rowData["formattedPrice"] as? String,
imgData = NSData(contentsOfURL: imgURL),
trackName = rowData["trackName"] as? String {
cell.detailTextLabel?.text = formattedPrice
cell.imageView?.image = UIImage(data: imgData)
cell.textLabel?.text = trackName
}
return cell
}
see also TableImage loader from github
The actual Apple reference documentation is quite comprehensive
https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/CreateATableView.html#//apple_ref/doc/uid/TP40015214-CH8-SW2
Scroll down until you see this part