what do tableview functions (CellForRowAtIndexPath and numberOfRowsInSection) do? - swift

I was viewing some videos on app development with swift, they used these functions but they did not explain about them much. What do they do exactly?
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
This function is used in the table view data source to specify how many table view cells you want in the given section. To get this function call (and the other calls), you need to set tableView.dataSource = self. Then you can implement this method to set a number of cells in the section.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
Use this function to get a UITableViewCell and customize the cell for when it is shown in the table. This method will also be called because of the data source property we set. Implement this and return a cell. Here is an example:
// This code is off the top of my head, sorry if the syntax or method names are off
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellForReuseIdentifier("ReuseIdentifier", indexPath: indexPath)
cell.textLabel?.text = "A title for the cell"
return cell
}

Related

How to use SkeltonView using RxSwift, RxCocoa

I wanna use SkeltonView in my app. But, it was described that we need to confirm to SkeltonTableViewDataSource to use it in README.
Is there any one who knows to use SkeltonView like tableView.rx.items.
If you want to show the skeleton in a UITableView, you need to conform to SkeletonTableViewDataSource protocol.
public protocol SkeletonTableViewDataSource: UITableViewDataSource {
func numSections(in collectionSkeletonView: UITableView) -> Int // Default: 1
func collectionSkeletonView(_ skeletonView: UITableView, numberOfRowsInSection section: Int) -> Int
func collectionSkeletonView(_ skeletonView: UITableView, cellIdentifierForRowAt indexPath: IndexPath) -> ReusableCellIdentifier
func collectionSkeletonView(_ skeletonView: UITableView, skeletonCellForRowAt indexPath: IndexPath) -> UITableViewCell? // Default: nil
func collectionSkeletonView(_ skeletonView: UITableView, prepareCellForSkeleton cell: UITableViewCell, at indexPath: IndexPath)
}

how to prevent a cell in tableview to be moved

I need to prevent last cell from second section from being moved and even to show the "right handle" that appears when tableview is editing.
I create cellsObject based upon a model which has an "identity" property in a Table with two cells. when using
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {}
I move from one array to the other based upon the section.
how and where should I block to that cell with that identity from being moved anywhere? I tried this inside cellForRow:
if model.identity == "DoNotMove" l {
cell.isEditing = false
cell.shouldIndentWhileEditing = false
cell.editingAccessoryType = .none
}
but is not working, tested the same inside the method
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {}
with crash result
Implement the data source method tableView(_:canMoveRowAt:)
func tableView(_ tableView: UITableView,
canMoveRowAt indexPath: IndexPath) -> Bool
and return false for the index path

Tableview creates five rows on one row?

I have a tableView with a custom cell that I have told should give me 5 cells in return of this custom cell. The question now is that I am running the app and getting five rows on one row. I have changed from default size of cell to custom but that is nearly the only thing I have done. So now I wonder what can create this kind of problem?
The code of my tableView looks like this.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "theoryCell") as! theoryTVC
return cell
}
So the problem is you are using custom cell with custom height but you are not setting height for row in table view method.
Try this:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return 100.0 //Choose your custom row height
}

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.

UITableViewController delegate gives error in swift

I am getting the error below while making a simple code for UITableView in Swift Xcode 6.1
/Users/classic/Documents/CIPL/Demo Projects/DemoTableView/DemoTableView/ViewController.swift:11:1: Type 'ViewController' does not conform to protocol 'UITableViewDataSource'
/Users/classic/Documents/CIPL/Demo Projects/DemoTableView/UIKit.UITableViewDataSource:3:48: Protocol requires function 'tableView(_:cellForRowAtIndexPath:)' with type '(UITableView, cellForRowAtIndexPath: NSIndexPath) -> UITableViewCell'
Anyone knows why this issue is occurring ?
Did you declared the mandatory functions of UITableVIewDataSource protocol in your class ?
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
You have to implement all mandatory protocol method for tableview.
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayData.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Define your cell and return it
}