Protocol UITableViewDataSource is not called from xib - Swift - swift

I don't know why my protocol UITableViewDataSource is not execute when i run the APP.
Here my xib design:
My init of class from xib is: CustomSearchDestination
#IBOutlet weak var searchText: UITextField!
#IBOutlet weak var tableView: UITableView!
let xibName = "CustomSearchDestination"
var destinationListLocal: [Destinos] = []
var SearchViewControllerView: SearchViewController?
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
xibSetup(xibName)
SearchViewControllerView = SearchViewController()
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
searchText.delegate = self
tableView.delegate = self
tableView.dataSource = self
}
override init(frame: CGRect) {
super.init(frame : frame)
xibSetup(xibName)
}
Inside of this class have implemented the protocol UITableViewDelegate and UITableViewDataSource
extension CustomSearchDestination: UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("El count es: \(destinationListLocal.count)")
return destinationListLocal.count
}
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedCell: UITableViewCell = tableView.cellForRow(at: indexPath)!
searchText.text = selectedCell.textLabel!.text
}}
extension CustomSearchDestination: UITableViewDataSource {
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
let index = indexPath.row as Int
if destinationListLocal.count == 0 {
if let str = destinationListLocal[index].desDestino {
cell.textLabel?.text = str
}
}
return cell
}}
Can someone point me my mistake?
EDIT: My hierarchy is:
I have a xib where i put other xib like this:
this is my class CustomViewHandler. So.. my class CustomSearchDestination is a class of the superClass CustomViewHandler.
At the same time is a class of CustomSearch
class CustomViewHandler : UIView {
var view: UIView!
var controllerView : UIView!
// Keyboard
var activeField : UITextField?
var isKeyboardVisible = false
var fixValue : CGFloat?
var keyboardSize : CGFloat = 280
fileprivate func loadViewFromNib(_ nibName: String) -> UIView {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: nibName, bundle: bundle)
// Assumes UIView is top level and only object in CustomView.xib file
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
return view
}
func xibSetup(_ nibName: String) {
view = loadViewFromNib(nibName)
// use bounds not frame or it'll be offset
view.frame = bounds
// Makes the view stretch with containing view
view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
addSubview(view)
}}

Related

Problem using an extension to load image into a custom cell

My last question for today. I'm too newby with swift to know what's happening but my problem is as follows:
I've use the next extension to load an image from a URL and it works when I use it in a imageView in a normal view, but when the imageView is inside of a custom cell It doesn't recognize de method so I can`t use the extension. What am I doing wrong? Thank you all in advances.
My code for the extesion is:
import UIKit
extension UIImageView {
func loadFrom(URLAddress: String) {
guard let url = URL(string: URLAddress) else {
return
}
DispatchQueue.main.async { [weak self] in
if let imageData = try? Data(contentsOf: url) {
if let loadedImage = UIImage(data: imageData) {
self?.image = loadedImage
}
}
}
}
}
And the code for the table where I try to use it into a custom cell:
import UIKit
class EventosCustomCellController: UITableViewCell {
#IBOutlet weak var imEvento: UIView!
#IBOutlet weak var txtNombreEvento: UILabel!
#IBOutlet weak var txtFechaEvento: UILabel!
#IBOutlet weak var txtEstadoEvento: UILabel!
}
class ListaEventosTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Eventos"
}
// MARK: - Table view data source
override func viewWillAppear(_ animated: Bool) {
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return eventos.contarEventos()
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "prototipoCeldaEvento", for: indexPath) as! EventosCustomCellController
let evento = eventos.buscarEventoPorID(id: indexPath.row)
cell.txtNombreEvento?.text = evento?.nombre
cell.txtFechaEvento?.text = evento?.fecha
cell.txtEstadoEvento?.text = evento?.tipo
if evento?.tipo == "deportivo"{
cell.backgroundColor = .blue}
else if evento?.tipo == "cultural"{
cell.aplicarFondoDegradado()
cell.backgroundColor = .green}
else{
cell.backgroundColor = .red}
cell.layer.masksToBounds = true
cell.layer.cornerRadius = 10
//There is no method loadFrom when I try to use as follows
cell.imEvento?.loadFrom(URLAddress: (evento?.imagenes![0])!)
return cell
}
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
}
After updating code my problem is that the cell isn`t growthn appropiately
I think, you better use UIImageView instead of UIView because your extension for UIImageView and you can access to this.
#IBOutlet weak var imEvento: UIImageView!
and then it is good to override the prepareForReuse method like this:
override func prepareForReuse() {
super.prepareForReuse()
imEvento.image = nil
}
you can read more about the prepareForReuse method from apple documentation

Custom Table View outlets throwing nil

I am trying to implement a simple iOS app using the VIPER pattern following this tutorial. The app is a simple table view which displays data from a json req
I have created the table view and can successfully show data from my object using the default cell.textLabel. I am however trying to create a custom table view cell and so created the nib and class. I have connected all the outlets up correctly to the class from the nib and the code for this class is as follows:
class CustomTableViewCell: UITableViewCell {
static let identifier = "CustomTableView"
static func nib() -> UINib {
return UINib(nibName: "CustomTableViewCell", bundle: nil)
}
#IBOutlet var level: UILabel!
#IBOutlet var levelNumber: UILabel!
#IBOutlet var progress: UIProgressView!
#IBOutlet var leftProgress: UILabel!
#IBOutlet var rightProgress: UILabel!
public func configure(levelString: String, levelNum: String, prog: Float, left: String, right: String) {
level.text = levelString
levelNumber.text = levelNum
progress.setProgress(prog, animated: true)
leftProgress.text = left
rightProgress.text = right
}
override func awakeFromNib() {
super.awakeFromNib()
}
}
The problem is, when I run the app, these outlets are erroring: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value on level.text inside the configure function. I can suppress this error by doing this in my view:
table.register(CustomTableViewCell.nib(), forCellReuseIdentifier: CustomTableViewCell.identifier) however this just shows an empty tableView without the custom rows or any data. If I do table.register(CustomTableViewCell.self, forCellReuseIdentifier: CustomTableViewCell.identifier) then this crashes. What way should I be doing it and how can I get the data to display in the tableView? relevant code added below:
let tableView: UITableView = {
let table = UITableView()
table.register(CustomTableViewCell.nib(), forCellReuseIdentifier: CustomTableViewCell.identifier)
table.isHidden = true
return table
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tableView)
tableView.delegate = self
tableView.dataSource = self
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableView.frame = view.bounds
label.frame = CGRect(x: 0, y: 0, width: 200, height: 50)
label.center = view.center
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return achievements.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: CustomTableViewCell.identifier, for: indexPath) as! CustomTableViewCell
cell.configure(levelString: "Level",
levelNum: achievements[indexPath.row].level,
prog: Float(achievements[indexPath.row].progress/achievements[indexPath.row].total),
left: String(achievements[indexPath.row].progress),
right: String(achievements[indexPath.row].total))
return cell
}
Outlet Setup:
Files Owner:
I appreciate this might not be the correct answer for everyone, but this is what worked for me. I was struggling with trying to debug this and for the sake of time, I concluded that just creating everything programmatically would be easier. There are numerous tutorials for this, but I took the following structure:
Stevia library used to set constraints for components and add subview
import Stevia
import UIKit
class CustomTableViewCell: UITableViewCell {
static let identifier = "CustomTableViewCell"
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupFieldsAndConstraints()
}
func setupFieldsAndConstraints() {
subviews {
customLabel
}
setupCustomLabel()
}
var customLabel: UILabel = {
let level = UILabel()
return level
}()
func setupCustomLabel() {
customLabel.style { l in
l.textAlignment = .left
l.textColor = .white
l.Top == 50
l.Left == 20
}
}
func setValues(object: Object) {
label.text = object.name
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
And this is referenced in your view as it normally would be. I chose to add mine to a function that encapsulates any table configuration and then called in viewDidLoad:
tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: CustomTableViewCell.identifier)
And then for the cellForRowAt:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: CustomTableViewCell.identifier) as! CustomTableViewCell
cell.setValues(object: Object[indexPath.row])
return cell
}

TextView custom subclass - textViewDidChange does not trigger

I'm trying to add an variable to my TextView subclass but I can't get textViewDidChange to work properly once I do that and I think it's because the delegate.
It works without the subclass and prints the itemOrderID. How can I get this to work with my custom subclass?
class textViewSub: UITextView {
var itemOrderID: Double = 0.0
}
class TextviewCell: UITableViewCell, UITextViewDelegate {
#IBOutlet weak var textView: textViewSub!
#IBOutlet weak var textViewLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
textView.delegate = self
}
func textViewDidChange(_ textView: textViewSub) {
let id = textView.tag
let text = textView.text
let itemOrderID = textView.itemOrderID
print(itemOrderID)
}
}
cellForRowAt code:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TextviewCell", for: indexPath) as! TextviewCell
cell.tag = indexPath.row
cell.textView.tag = indexPath.row
cell.textView.itemOrderID = checklistData[indexPath.section].checklistItems?[indexPath.row].itemOrderID ?? 0.0
cell.textView.delegate = cell
return cell
}
Your textViewDidChange function does not match the UITextViewDelegate protocol.
It should be:
func textViewDidChange(_ textView: UITextView) {
guard let sub = textView as? textViewSub else { return }
let id = sub.tag
let text = sub.text
let itemOrderID = sub.itemOrderID
print(itemOrderID)
}

ViewController does not conform to protocol 'UITableViewDataSource

**Where's the wrong?
ERROR "SecondViewController does not conform to protocol 'UITableViewDataSource"**
import UIKit
class SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
{
#IBOutlet weak var tableView: UITableView!
var videos:[video] = [video]()
#IBOutlet weak var menuButton: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
if self.revealViewController() != nil {
menuButton.target = self.revealViewController()
menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
let model = videoModel()
self.videos = videoModel().getVideos()
self.tableView.dataSource = self
self.tableView.delegate = self
}
func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return videos.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "BasicCell", for: indexPath)
let videoTitle = videos[indexPath.row].videoTitle
//customize the cell
cell.textLabel?.text = videoTitle
return cell
}
}
}
Perhaps, You did not connect UIViewController and UITableView.
In Stroyboard,
Place your mouse over UITableView, Drag to UIViewController using right button. And then you can check outlets options, datasource and delegate. Check these.

Why won't my subclass of UITableViewCells appear when run?

When I wireframe it, the cell is nested above the TableView. My project structure is that I have a main view controller and a subclass of a TableView that's also using a TableViewCell that I subclassed. Everything is in place, I have no idea why it's not showing.
import UIKit
class ViewController: UIViewController{
#IBOutlet weak var tv: TaskTableView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
My subclass of UITableView.
import UIKit
class TaskTableView: UITableView, UITableViewDelegate, UITableViewDataSource {
var title: String = ""
var detail: String = ""
var time: NSDate = NSDate()
var data = ["asdf","asdf","sadf"]
init(frame: CGRect, title: String, detail: String, time: NSDate)
{
self.title = title
self.detail = detail
self.time = time
super.init(frame: frame, style: UITableViewStyle.Plain)
}
required init?(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return data.count
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
tableView.registerNib(UINib(nibName: "NewTVCell", bundle: nil), forCellReuseIdentifier: "toDoCell")
let cell = tableView.dequeueReusableCellWithIdentifier("toDoCell", forIndexPath: indexPath) as! NewTVCell
cell.title.text = data[indexPath.row]
return cell
}