Setting XIB inside custom cell tableView issue - swift

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?

Related

Custom UITableViewCell has no initialiser error on only one of the two created classes. Swift

I'm adding a new TableVIewto the project and I'm also creating a custom class fro the cell. I'm doing as usual : New file/ Cocoa Touch Class / UITableViewCell / name. As soon as I start adding properties I get the error dough properties are declared as!. It doesn't happen on my other custom cell class. Can you see what am I doing wrong with this new class?
No error on this class :
import UIKit
class CalendarTableViewCell: UITableViewCell {
#IBOutlet weak var dayLabel: UILabel!
var cellId: String!
var cellWeekday: Int!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func prepareForReuse() {
super.prepareForReuse()
// Set your default background color, title color etc
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
and the new class that makes xCode complain:
import UIKit
class ProductTableViewCell: UITableViewCell {
#IBOutlet weak var productImageView: UIImageView!
#IBOutlet weak var productIDLabel: UILabel!
#IBOutlet weak var productIDInfoLabel: UILabel!
#IBOutlet weak var categoryLabel: UILabel!
#IBOutlet weak var categoryInfoLabel: UILabel!
#IBOutlet weak var nameLabel: UILabel!
#IBOutlet weak var nameInfoLabel: UILabel!
#IBOutlet weak var priceLabel: UILabel!
#IBOutlet weak var priceInfoLabel: UILabel!
#IBOutlet weak var quantityLabel: UILabel!
#IBOutlet weak var quantityInfoLabel: UILabel!
var productImage: UIImage!
var category: String!
var productId: String!
var name: String!
var price: String
var vendor: String!
var cellId: Int64
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
It's something I overlooked for sure but I can't spot it.
What should I check?
Your var cellId: Int64 is not initialized. In first cell you explicitly specified that you will initialize it before use with exclamation mark, but not in the second cell.

Swift: set #IBOutlet static

I want change #IBOutlet text value from another class
How i can set #IBOutlet to static ?
Below code not work:
#IBOutlet internal static var boxGender: UIView!
You can set a static value of your controller and get all values from them. But this is not a right way to reach your outlets. You should pass your controller to other class instance.
class Test: UIViewController {
var boxGender: UIView!
static var instance: Test?
override func viewDidLoad() {
super.viewDidLoad()
Test.instance = self
}
}
Test.instance?.boxGender
An IBOutlet cannot be static. (The compiler will give the error Only instance properties can be declared #IBOutlet)
class SomeViewWithALabel: UIView {
#IBOutlet weak var myLabel: UILabel!
// ... methods and properties
}
class MyController: UIViewController {
#IBOutlet weak var someViewWithALabel: SomeViewWithALabel!
//...
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
someViewWithALabel?.myLabel.text == "Custom text"
}
}

How to connect outlets and actions in Swift / MacOS programaticly

I have a simple example.
I connected the left button1 and label1 with ctrl-draging it to the contollerclass.
How can I do the same for the right button2 label2 programaticly (without ctrl-draging)
That´s my code:
class ViewController: NSViewController {
#IBOutlet weak var label1: NSTextField! //connected with ctrl-drag
#IBOutlet weak var button1: NSButton! //connected with ctrl-drag
#IBOutlet weak var label2: NSTextField! //not yet connected
#IBOutlet weak var button2: NSButton! //not yet connected
#IBAction func button1Pressed(_ sender: Any) //connected with ctrl-drag
{ label1.stringValue = "button-I"
button1.title = "pressed"
}
#IBAction func button2Pressed(_ sender: Any) //not yet connected
{ label2.stringValue = "button-II"
button2.title = "pressed"
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
If you want to use storyboard and lay out all your elements there, you can't really avoid the ctrl drag (or just drag if you open the connections inspector on the right).
You could however create your second button programmatically in code and not use the storyboard at all for it. Then programmatically add your constraints too.
You can also add the action of the button programmatically (using addTarget) if you would like but that would require at least the IBOutlet to be setup in order to have a reference to the button.

How to use UIButtons in a Cell with Functions?

I created a custom cell added the actions and outlets.
I want to be able to get the cell indexpath when the cell button is pressed.
I am send data to my parse server and I need to the cell indexPath.row so when someone clicks the button the count for likes or dislikes goes up.
My Custom Cell :
class myTVC: UITableViewCell {
#IBOutlet weak var greenUp: UIButton!
#IBOutlet weak var redDown: UIButton!
#IBOutlet weak var imageViewTVC: UIImageView!
#IBOutlet weak var upVote: UILabel!
#IBOutlet weak var downVote: UILabel!
#IBOutlet weak var saveButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
#IBAction func voteDownPressed(sender: UIButton) {
print("downVote")
}
#IBAction func voteUpPressed(sender: UIButton) {
print("upVote")
}
}

UITextfield value for each indexpath.row

My uitableview consists of 10 cells and each cell has 2 uitextfield's.
I need to take values from each uitextfield in each cell and add it to an array
// my custom cell
class MatchTableViewCell: UITableViewCell, UITextFieldDelegate {
#IBOutlet weak var Team2Score: UITextField!
#IBOutlet weak var Team1Score: UITextField!
#IBOutlet weak var Team2: UILabel!
#IBOutlet weak var Image2: UIImageView!
#IBOutlet weak var Image1: UIImageView!
#IBOutlet weak var Team1: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Make sure in the cell initializer for tableviewcontroller that your using your custom cell name and that you've set your identifier correctly. Then just initalize a cell and say something to the effect of
cell.Team2Score.text = "100"
you will need a global array to hold the strings. So..
var wordArray: [String] = []
you will need to add a button within the cell so when you hit the button it will add (append in the input of string (letters) to the array.
You need to add a tag to the button to know which cell it was clicked in.
button.tag = indexPath.row
#IBaction button(sender: UIButton){
var team2Words = Team2Score.text
wordArray.append(team2Words)
// do the samething for Team1Score
// you can use and if statement to check if one or the other one is empty
}
This will give you a lead