Pass outlet Data from TableViewCell to ViewController.swift - swift

Getting error Unexpectedly found nil while unwrapping an Optional value
while trying to pass data from tableviewcell to viewcontroller
DataCell.swift
#IBOutlet weak var containerView: UIView!
viewcontroller.swift
let dataCell = DataCell()
override func viewDidLoad() {
super.viewDidLoad()
dataCell.containerView.layer.cornerRadius = 10.0
}

#IBOutlet weak var containerView: UIView!
You have this IBOulet connection in a xib file or a in a stotryboard. And you are creating the cell programmatically in the view controller. When you create a cell programmatically its IBOutlet connections will be nil. Either created the containerView programmatically without IBOutlet in DataCell class or change the cornerRadius of the containerView in cellForRowAtIndexPath method.

Related

Unexpectedly found nil in swift with UIProgressView

I've created a progressView and linked it like this:
And here is what I do in my view controller:
class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
#IBOutlet weak var progressView: UIProgressView!
#IBOutlet weak var KronosWebsite: WKWebView!
override func loadView() {
//initialize the webview
progressView = UIProgressView()
progressView.progress = 1.0
KronosWebsite = WKWebView()
self.view = KronosWebsite
self.view.addSubview(progressView)
}
}
And I've got
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional
in this line progressView.progress = 1.0
Add super.loadView() when you use loadView for a vc that originally inside storyboard
override func loadView() {
super.loadView()
or insert all code inside viewDidLoad , also don't re-init
progressView = UIProgressView()
KronosWebsite = WKWebView()
as they already initiated from storyboard
The docs say to never override loadView if your view controller comes from a storyboard or xib:
loadView()
If you use Interface Builder to create your views and initialize the view controller, you must not override this method.
Everything that you're currently doing in loadView can be done in the storyboard anyway, so just remove the loadView method and connect your UIProgressView and WKWebView to your outlets in your storyboard. You can also set the progress view's initial progress to 1.0 inside the storyboard.

IBOutlet nil for custom view xib that is embedded in other xib

I am adding a subview(xib) to the view controller. The subview contains other subview(xib).
When the app loads, both the subviews are loaded but , the last child subview has all it IBOutlets as nil.
Below is the simple code with Viewcontroller and two views.
The loadUI() method is printing nil.
Heres the full code. https://github.com/Laxmanraju/SubnibbedView
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let cnatainer = Bundle.main.loadNibNamed("ContainerView", owner: self, options: nil)?.first as! ContainerView
self.view.addSubview(cnatainer)
cnatainer.loadUI()
}
}
class ContainerView: UIView {
#IBOutlet weak var detail: DetailOptionView!
func loadUI(){
print(detail.imageView). // prints nil
}
}
class DetailOptionView: UIView {
#IBOutlet weak var imageView: UIImageView!
}

Setting XIB inside custom cell tableView issue

I have followed this this tutorial
How to visualize reusable xibs in storyboards using IBDesignable
I have added a label inside the view to change the label and I'm calling the xib inside the custom cell for tableView. So I'm facing this issue,
fatal error: unexpectedly found nil while unwrapping an Optional value
While I’ve added this code inside the view and linked it
#IBOutlet weak var priceLabel: UILabel!
This is how I'm calling it inside the custom cell.
TableViewcell code:
class ProductListBigTableViewCell: UITableViewCell {
#IBOutlet weak var productImage: UIImageView!
#IBOutlet weak var productName: UILabel!
#IBOutlet weak var productQuantity: UILabel!
#IBOutlet weak var productAddButton: BaseButton!
#IBOutlet weak var prodcutPriceView: PriceView!
override func awakeFromNib() {
super.awakeFromNib()
productFirstName.text = "first product"
productFirstQuantity.text = "4 p"
prodcutPriceView.priceLabel.text = ""
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
I can figure that the view is not initialised yet so it is nil. I could not find a way or resource how to change a value inside XIB inside the UITableViewCell. So how I can change it and initial the view for the XIB?

Delegate used to control a button inside a cell not working

I have a custom UIView with 3 tableviews, the tableviews are self-sizing depending on the content they have, and they a show a different nib when they are empty than when they have content. All of this is embedded in a scroll view and it's working already, my view scrolls and the tableviews display the content appropriately and they autosize properly, the scroll view's size is adjusting without problems either.
In two of the tableviews when the tableviews have items there are some buttons in the cell nib that I want to access along with the index of the cell clicked inside the View controller where I have defined the Table view.
Here in the pic I'm showing the tableViews each with one item, the Canastas tableView has just a delete item button and the Productos tableView has a delete item button and a stepper to increase or decrease the amount.
I found a solution involving delegates in StackOverflow
Get button click inside UI table view cell, which I implemented. But for some reason it isn't working and the code in the delegate method isn't being executed.
Here is my code:
CanastasViewCell
import UIKit
protocol CanastasViewCellDelegate: class {
func closeButtonTapped(at index: IndexPath)
}
class CanastasViewCell: UITableViewCell {
#IBOutlet weak var imagenProducto: UIImageView!
#IBOutlet weak var nombreProducto: UILabel!
#IBOutlet weak var descProducto: UILabel!
#IBOutlet weak var precioProducto: UILabel!
#IBOutlet weak var closeButton: UIButton!
weak var delegate: CanastasViewCellDelegate?
var indexPath: IndexPath!
override func awakeFromNib() {
super.awakeFromNib()
}
#IBAction func eliminarProducto(_ sender: AnyObject) {
self.delegate?.closeButtonTapped(at: indexPath)
}
}
CarritoViewController
import UIKit
class CarritoViewController: UIViewController, UITableViewDelegate,
UITableViewDataSource, CanastasViewCellDelegate {
func closeButtonTapped(at index: IndexPath) {
print("Button tapped at index:\(index)")
}
It looks like you're not setting the delegate inside CarritoViewController. You'll want to make sure that you set cell.delegate = self inside func tableView(UITableView, cellForRowAt: IndexPath).

Static Table View with dynamic text

I have a static table view, but I want the last cell to say "Logout, (username here)"...can I even do that with a static table view?
The language I am using is Swift.
So far, I've tried to use cellForRowAtIndexPath and only have it return anything when the indexPath.row is the last cell. That doesn't work. Furthermore, it changes all of the other cells and, on top of that, they lose their segues to the other views...
Yes, you can update controls in static cells in UITableViewController's UITableView. When you have a static table, you can just create IBOutlet references to the controls in the cells and update those outlets directly without use of any UITableViewDataSource methods. You can effectively just ignore the fact that it is a UITableView and treat these labels as if they were placed directly on the view in question. Just hook up the controls in the static cells to IBOutlet references in the view controller.
For example:
class ViewController: UITableViewController {
#IBOutlet weak var label1: UILabel! // in first cell
#IBOutlet weak var label2: UILabel! // in second cell
#IBOutlet weak var label3: UILabel! // in third cell
override func viewDidLoad() {
super.viewDidLoad()
label1.text = "foo"
label2.text = "bar"
label3.text = "baz"
}
}