the tableView delegate functions do not run Swift4

1

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)
    }
}

}

    
asked by Jhonly Junior Garcia Pitoy 27.06.2018 в 03:44
source

2 answers

0

Make sure you have indicated who is the delegate of the table and the datasource. If you get 0 the number of sections, you do not do anything else, so self.listFavorites.count must be different from 0

    
answered by 27.06.2018 в 07:32
0

Dude, suddenly there is a problem in the construction of the board. I suggest you add the table view's constructor to your FavoriteTableView class.

public override init(frame: CGRect, style: UITableViewStyle) {
    super.init(frame: frame, style: style)
}

and in objective c you would initialize it like this:

FavoriteTableView *tabla = [[FavoriteTableView alloc] initWithFrame:<tu_tabla>.bounds
                                                              style:<tu_tabla>.style];

A "tu_tabla" I mean the one you add in your view.

    
answered by 29.06.2018 в 02:03