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
}
Related
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)
}
I am having an issue where the section footers for my UITableView are not taking up the full width of the table view. I have a custom cell that uses the text label for a descriptor and the accessory view for a UISwitch. It seems like the footer is using the width of the content view of these cells (though I should note it's slightly off), which does not include the accessory view.
Here's an image of the issue.
Is there something that is known to cause this that I can fix without having to do some weird workaround? I only phrase it this way as I just want the default behavior.
I tried this in a sample table view and it worked for me very well. I will just post what I did and maybe you can check if you have done the same.
Firstly, I set the Table View Style to grouped in the Interface builder:
Next, code for the table view data source and delegate methods:
ViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Cell implementation here
return UITableViewCell()
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "DATES"
}
func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return "If both a delivery date and notification date are chosen, the notification date must be after the delivery date."
}
}
Note
Use tableView(_:, titleForHeaderInSection:) and tableView(_:, titleForFooterInSection:) for simple text as header and footer
Use tableView(_:, viewForHeaderInSection:) and tableView(_:, viewForFooterInSection:) when you need a non-text header and footer or a complex header/footer.
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.
I have started my first app, and I want the app to have a view that works as a to-do list, however, I'm getting type 'ThirdViewController' does not conform to protocol UITableViewDataSource as an error in my code. I already looked at a similar thread but found no solution there
import UIKit
class ThirdViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
{
var list = ["Math Homework", "English Project", "Bio Quiz"]
#IBOutlet weak var myTableView: UITableView!
override func viewDidLoad()
{
super.viewDidLoad()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return list.count
}
public func tableView(tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell
{
let cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "cell")
cell.textLabel?.text = list[indexPath.row]
return cell
}
}
As specified in the Apple Developer documentation, you need the following methods:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
You instead used these:
public func tableView(tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
Remove public from both methods and it should work. Also, add _ before tableView:.
Reference:
https://developer.apple.com/reference/uikit/uitableviewdatasource
EDIT:
The methods that I described are for Swift 3. In case you are using Swift 2.3, these should be your methods:
func tableView(tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
Almost the same that you used in the first place, but without public.
Implement the required protocol methods for UITableViewDataSource and UITableViewDelegate.
you are missing;
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
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
}