How to dynamically add a cell to UICollectionView - swift

I want to add a cell in UICollectionCell on run time. What is the best way that not affected performances?
And It's my sample cell class:
class PersonViewCell: UICollectionViewCell {
#IBOutlet weak var coverImg: UIImageView!
#IBOutlet weak var nameLbl: UILabel!
#IBOutlet weak var titleLbl: UILabel!
var p: Person!
func configureCell(p: Person) {
self.titleLbl = p.title
nameLbl.text = p.name
coverImg.image = UIImage(named: "image-not-available")
}
}

Make a new instance variable in your class to store an array and save your JSON results to it. Then use that to return the number of cells in numberOfItemsInSection, and draw the cells in cellForItemAtIndexPath. If you want to update after that, just call collection.reloadData()
Also, assuming you're making your network request on a background thread, make sure you call collection.reloadData() on the main.

Related

Why isn't this code in ViewDidLoad executing?

I have two text fields, one button, and four labels.
When you push the button, it should update the four labels.
Label 1: name
Label 2: email
Label 3: name (same data as label 1)
Label 4: email (same data as label 2)
I was experimenting under viewDidLoad() to see if I can make another var and update my last label. No error when I run the code, but it turns out "empty" in the simulator.
Can anyone explain why?
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var emailInput: UITextField!
#IBOutlet weak var nameInput: UITextField!
#IBOutlet weak var label: UILabel!
#IBOutlet weak var label2: UILabel!
#IBOutlet weak var label3: UILabel!
#IBOutlet weak var label4: UILabel!
#IBAction func actionButton(_ sender: Any) {
var text = nameInput.text
var email = emailInput.text
label.text = text
label2.text = email
label3.text = text
}
//where i experimented:
override func viewDidLoad() {
super.viewDidLoad()
var email2 = emailInput.text
label4.text = email2
}
}
Change
label4.text = email2
To
label4.text = "howdy"
Now you will see something in your interface.
The viewDidLoad method called in the beginning perfectly. But the label not showing any data because initially the textfield has no value. That's the label not contains any data.
Please set any value to the label initially. Like
label4.text = "Something"
Hope you can understand that the viewDidLoad is called perfectly.

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

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