hi friends I am new in the beautiful world of good programming without more: I am trying to fill a tableView through its delegate functions and it happens that of these functions only executes:
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.listFavorites.count
}
and here is the complete code: (I use this class as a pod in an obj c project and that's why I use the @obj).
import UIKit
import Foundation
@objc
public protocol FavoriteTableViewDelegate : NSObjectProtocol {
func didSelectRowAt(indexPath: IndexPath)
}
@objc
public class FavoriteTableView: UITableView, UITableViewDelegate, UITableViewDataSource {
public var heightCell: CGFloat = 50.0
public var listFavorites: [Favorite] = [Favorite]()
public var delegateFavorite: FavoriteTableViewDelegate?
public var listTitles: [String] = [String]()
public var listSubTitles: [String] = [String]()
@objc
public func configure(listTitles: [String], listSubTitles: [String], heightCell: CGFloat = 50.0, bounces: Bool = false){
self.listTitles = listTitles
self.listSubTitles = listSubTitles
var listFavoritos: [Favorite] = []
if listTitles.count >= 1 {
for i in 0...listTitles.count - 1 {
let favorite = Favorite()
favorite.title = listTitles[i]
favorite.subtitle = listSubTitles[i]
listFavoritos.append(favorite)
}
}
self.configure(list: listFavoritos, heightCell: heightCell, bounces: bounces)
}
@objc
public func configure(list: [Favorite], heightCell: CGFloat = 50.0, bounces: Bool = false){
self.delegate = self
self.dataSource = self
if heightCell <= 50.0 {
self.heightCell = 50.0
} else {
self.heightCell = heightCell
}
self.listFavorites = list
self.backgroundColor = .clear
self.separatorStyle = .none
self.bounces = bounces
self.reloadData()
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.listFavorites.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print(self.frame.size.width)
if self.listFavorites.count == 1 {
let cell = FavoriteCell(type: .one, width: self.frame.size.width, height: self.heightCell, reuseIdentifier: "cell")
cell.config(title: self.listFavorites[indexPath.row].title!, subTitle: self.listFavorites[indexPath.row].subtitle!)
print("uno",cell.frame.size.width)
return cell
} else if indexPath.row == 0 {
let cell = FavoriteCell(type: .first, width: self.frame.size.width, height: self.heightCell, reuseIdentifier: "cell")
cell.config(title: self.listFavorites[indexPath.row].title!, subTitle: self.listFavorites[indexPath.row].subtitle!)
print("0",cell.frame.size.width)
return cell
} else if indexPath.row == self.listFavorites.endIndex - 1 {
let cell = FavoriteCell(type: .last, width: self.frame.size.width, height: self.heightCell, reuseIdentifier: "cell")
cell.config(title: self.listFavorites[indexPath.row].title!, subTitle: self.listFavorites[indexPath.row].subtitle!)
print("2",cell.frame.size.width)
return cell
} else {
let cell = FavoriteCell(type: .normal, width: self.frame.size.width, height: self.heightCell, reuseIdentifier: "cell")
cell.config(title: self.listFavorites[indexPath.row].title!, subTitle: self.listFavorites[indexPath.row].subtitle!)
print("1",cell.frame.size.width)
return cell
}
}
@objc
public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return self.heightCell
}
@objc
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.delegateFavorite?.didSelectRowAt(indexPath: indexPath)
let cell = self.cellForRow(at: indexPath)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
cell?.setSelected(false, animated: true)
}
}
}