Custom cell / table view - swift

I am trying to learn tableView and custom cell. I was able to find some tutorials.
However, I need to be able to have informations to go from a cell to another, when the users are editing informations.
I also have a problem implementing the code like an Alert view to work on a cell, even know it's working on a view controller.
Can someone guide me toward a tutorial, as I have difficulty to found one.

As I said in my comment, you're asking too many questions in one, anyway this is a small TableView tutorial:
First thing first, you need to create your tableview:
var tableView: UITableView!
then you'll add it to your view:
//Put these in your view did load
//Change tose vaues as you please
tableView = UITableView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width, height: 400), style: .plain)
view.addSubview(tableView)
So you'll need to register the cells:
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell") //Choose the identifier you want but always use the same within a single Table view
You'll have to set the delegate and the data source:
//put these in your view did load
tableView.delegate = self
tableView = self
And your class will need to conform to these protocolls, so, you'll add the UITableViewDelegate and the UITableViewDataSource to it:
class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{}
So you'll have to set the protocol stubs as well:
//Here you'll decide the number of cells your tableView will have
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {}
//Here you'll decide the desired output of your cell (text, color ecc...)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//You have to create the cell first, it can be done as follows:
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) //Notice how I'm using the same identifier
return cell
}
There are plenty of other protocol stubs like:
//Use this to determine the behaviour of the cell when you tap it
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {}
//Use this to set the cell's height
private func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{}
You can find more in the apple documentation.

Related

How to show separate View(box) in between tableview in Swift

In this screen how to show (blue view) in between tableview rows
design image
code: in storyboard design i have given all static data in labels and images so with this below code i am getting all cells like above screen shot, but after three cells how to show blue box view, please suggest me
import UIKit
class ViewController: UIViewController , UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "BidCell", for: indexPath)
return cell
}
There are two ways to do that:
Use a different UITableViewCell class (probably what you are looking for?)
Use sections
How?
You can either create a new UITableViewCell prototype cell in your storyboard or do it programmatically.
Create a custom UITableViewCell as such:
class OfferTableViewCell: UITableViewCell {
}
// If you are not using storyboards, add the following code
// in your viewDidLoad
tableView.register(OfferTableViewCell.self, forCellReuseIdentifier: "your_cell_id")
Then, you can dequeue the newly created cell at any index as such:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 10 // Index of the different cell
let cell = tableView.dequeueReusableCell(withIdentifier: "your_cell_id", for: indexPath) as! OfferTableViewCell
// Do cell configuration here
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "BidCell", for: indexPath)
return cell
}
}
Keep in mind that this cell is going to take the place of another cell if you are using an array as datasource, so using myArray.count for your numberOfRowsInSection would result in the last array element missing. You will have to take that into account.
Resources
Using multiple custom cells in a TableView
UITableView sections
Custom header for TableView sections

How to make Custom Xib Cell Resize Correctly

I'm trying to make a custom xib cell resize based on really how much information the user enters.
I have tried setting the constraints of the xib cell, like this Constraints
I only have top and bottom constraints on the Label and the Date.
In the viewDidLoad() of the viewcontroller containing the cell and tableview i have
self.tableView.rowHeight = UITableView.automaticDimension
and I also have set
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 250
}
GIF of Resize Not Working

Swift adding clickable tableview

When I create a tableview, I cannot click on any of the items I put on the tableview. I was wondering how can I create a tableview that has every item clickable and when the user clicks on an item( say a city name, for example) it redirects the user to a different viewcontroller. ( for example if there are 22 clickable items in the tableview, there will be a total of 22 new different viewcontrollers )
Thank you very much in advance!
There are three major functions that a UITableViewDataSource must contain for the table view to work properly with user interaction (Pressing each row, for example). These functions are:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
The function you are looking to use is the third one. It is called when a user selects a certain row by tapping it on screen. You can find out the row index with 'indexPath'.
If you would like to go to 22 different view controllers, you need to create a manual segue between each one and label them accordingly. Then, you would want to call each individual segue depending on which row was selected inside of that third function! You can call a segue with an identifier with the performSegue() function.
Note that the class that contains these functions must be of type UITableViewDataSource, and you should tell the table view that it is the data source in the ViewDidLoad() function like so:
tableView.dataSource = self
Here is what a simple code can look like:
import UIKit
class viewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var identifiers = [String]()
override func viewDidLoad() {
// fill your identifiers here
tableView.delegate = self
tableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 22
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "yourCellIdentifier") as! yourCell
// fill your cell's data in here
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
// here you can use someThing like an array of your segue identifiers
self.performSegue(withIdentifier: identifiers[indexPath.row], sender: self)
//Or you can just implement a switch with every case doing what you want that cell to do which i don't recommend if you have 22 rows
}
}

Swift: Add cells containing a custom label to an UITableView

How do I programmatically add cells to a UITableview and fill the cells with data from myArray[cellNumber].
The data in the array is of type String. The tableview is just an UITableView connected with an outlet.
All the examples I've found is either +30 lines or doesn't work...
I'm using swift 4 and UIKit.
In Xcode, use "File > New > File > Cocoa Touch Class".
Use UITableViewController as a base class
You will find a big template, just implement:
numberOfSections(in tableView: UITableView) -> Int, make it return 1. You just need one section for now.
tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int, make it return the size of your array
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell. Uncomment it, implement it.
NOTE: To implement tableView(_:cellForRowAt:), you must in your storyboard register a cell, and use its name in this function. Or programmatically register a cell using register(_:forCellReuseIdentifier:) .
Here is a more comprehensive guide iOS Getting Started Guide UITableView
Implementation example:
override func numberOfSections(in tableView: UITableView) -> Int {
return 1 // Only one section
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// "cell" is registered in the Storyboard
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
// The registered cell, has a view with tag 1 that is UILabel as an example
// IndexPath is a data structure that has "section" and "row"
// It located the cell in your tableview/collectionview
(cell.viewWithTag(1) as? UILabel)?.text = myArray[indexPath.row]
return cell
}
1.Your ViewController must conform to the UITableViewDelegate, UITableViewDataSource.
That means your class file would look like this
class MyCustomViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
2.You must assign the dataSource and delegate properties of your UITableView object to your viewController either from the Storyboard by dragging or in the code in viewDidLoad for example by typing:
myTableView.delegate = self
myTableView.dataSource = self
3.Your class must override the UITableView required delegates/datasource methods numberOfRowsInSection and cellForRowAt:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = myArray[indexPath.row]
return cell
}
Note that to use dequeReusableCell you must set a Reuse Identifier for the cell in the storyboard file.

swift tableview image and label

I can't seem to figure how to have a tableview that contains a subject, body and image so that if there is not an image to have the subject and body together. see imageauto lay with table prototype
I would like the subject and body together when there is an image in the database and then if there is an image, then subject image body
pin the labels to the top and bottom of the cell. When you don't have an image you set the imageView.isHidden = true. You also return the regular cell height minus the imageView.frame.size.height in UITableViewDelegate.tableview(_:heightForRowAt:).
If you don't like approach just make two different tableViewCells and deque the one with the image when you have an image and the one without the imageView when you don't have an image.
You can do this in your dataSource methods for the tableview. Something like this should work:
//Model:
var model = [Stuff]
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myprototype", for: indexPath)
cell.subjectLabel.text = model[indexPath.row].subject
cell.bodyLabel.text = model[indexPath.row].body
//assuming the image is an optional
if let image = model[indexPath.row].image {
cell.imageView.image = image
}
return cell
}
You might have to set the autolayout constraints for the imageView to have a low compression resistance.
You might also want to make the cells resize by implementing the following in the UITableViewDelegate methods:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}