I added a Table View Controller to my storyboard. Then i set the Class of the Table View Controller to my class SubscriptionsTableViewController: UITableViewController
Now i want to populate it with a cell i've made.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.dequeueReusableCellWithIdentifier("subscriptionCell") as! SubscriptionsTableViewCell
return cell
This gives me Value of type SubscriptionsTableViewController has no member dequeueReusableCellWithIdentifier
How do i access the dequeueReusableCellWithIdentifier in TableViewController class? Shouldn't i be able to use self.dequeueReusableCellWithIdentifier since i've set the class in Storyboard?
dequeueReusableCellWithIdentifier is not UITableViewController method. It is UITableView method
So, you need
let cell = tableView.dequeueReusableCellWithIdentifier("subscriptionCell") as! SubscriptionsTableViewCell
Check the documentation first.
Be sure to register SubscriptionsTableViewCell as cell class of your table view.
Related
I have a view controller with a tableview inside it , in the table view i implemented a collection view with horizontal scrolling, the data appears correctly but i want to go to another view controller when i click on the collection view cell passing a string to that view , how can i handle this process .
thanks in advance
You have two choice
1) Implement Collection view delegate & datasource inside view controller. You can do this in tableview's cellForRowAt method like cell.collectionview.datasource = self
2) You can create delegate / closure to pass data from Collection View -> TableView Cell --> UIViewController
Hope it is helpful And let me know if you need example
EXAMPLE
As you have finding difficulties to understand theoretically here is example
suppose in your view controller
You have tableview datasource like below
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:"identifer", for: indexPath) as! YourTableViewCell
// You have collection view inside tableview cell so
cell.yourCollectionViewObject.dataSource = self
cell.yourCollectionViewObject.delegate = self
return cell
}
Implement or Move the datasource & Delegate in Your view Controller and push or present view controller you want to
This is my first time using TableView's and I've already hit a road block.
I've got a View Controller with a Container inside. The Container View has an embedded Table View Controller. I have populated the table view controller using the below code. I have set the datasource and the delegate to the Table View Controller, set the Custom Class of the Table View controller to ORMTableVC and set the Table View cell Identifier to cellIdentifier.
My problem is at runtime the View Controller is just blank and I cannot see any Table lines or data.
Any help will be greatly appreciated.
import Foundation
import UIKit
class ORMTableVC : UITableViewController {
let cellIdentifier = "cellIdentifier"
var Array = [String]()
override func viewDidLoad() {
super.viewDidLoad()
Array = ["Reps", "Weight", "RPE", "Fatigue", "Potential Max", "Fatigue Weight"]
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Array.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let Cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
Cell.textLabel?.text = Array[indexPath.row]
return Cell
}
}
StoryBoard Setup
Built App
After looking through many tutorials managed to fix this. Seemed like the code wasn't the issue it was the storyboard setup.
On the Table View Cell the identifier needed to be cellIdentifier not Cell.
I have a static table with one static section. Other sections are dynamic.
I create Table Section and Table Cell for dynamic section. Set identifier for Cell, set custom class for it and even do:
self.tableView.registerClass(UncheckedStoreTableViewCell.self, forCellReuseIdentifier: "StoreCell")
if i don't register it with code, then i get:
'unable to dequeue a cell with identifier StoreCell - must register a
nib or a class for the identifier or connect a prototype cell in a
storyboard'
So when i use this:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.section == 0 {
return super.tableView(tableView, cellForRowAtIndexPath: indexPath)
}
let cell = tableView.dequeueReusableCellWithIdentifier("StoreCell", forIndexPath: indexPath) as! UncheckedStoreTableViewCell
return cell
}
It works. But if i'm trying to change label: cell.myLabel.text = "one"
or just print(cell.myLabel) got
BAD_INSTRUCTION
You can definitely use dynamic cells in a static table view.
Don't expect a static table view to register your cell's identifier for you. Just do it yourself.
Do you have outlets in the cell class to some view in interface builder? If I were you I wouldn't expect the table view to know about that. It will instantiate your cell class, and that's it. No outlets will be set. I think this is related: load nib in view subclass
By the way, if you've defined a custom .nib for your cell, there's this method: registerNib(_:forCellReuseIdentifier:)
You do not need to register your cell in code.
You have correctly set the identifier of the cell, however this is not enough. In addition to this you also need to open Identity Inspector for your cell and set the class of the cell to be UncheckedStoreTableViewCell. Here is an image showing you where you should set it:
Without this step Xcode will not be able to correctly associate your cell identifier with your custom cell as it doesn't know anything about it!
i have a static table view with 2 sections. Each section has two rows. The first row is a date picker cell (DVDatePickerTableViewCell). Now i put manually a textfield into the second cell.
In the view controller i load the cells with this code:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let CellID : NSString = "Cell"
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(CellID) as UITableViewCell
if indexPath.section == 0 && indexPath.row == 0 {
return cells[indexPath.section][indexPath.row] as UITableViewCell
}
return cell
The Cell with the identifier "Cell" has the textfield in it. But when the table view is load, the first cell works perfectly but the others are just empty cells. How can i return the cells with the "style", which i created in the storyboard?
Thanks in advance!
In case you would like to use static cells in your table view, make sure that "Static cells" is selected in your table view settings:
In this case you don't need to implement override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell, table view will handle cells loading and displaying automatically.
To link any outlets from Interface Builder to your code you don't even have to create custom classes for your cells, you can link your views from table view's cells in Interface Builder directly to your view controller:
I know this is an issue with importing a class but I'm not sure which one I need.
Currently I have public class postWithTitleAndImageTableViewCell: UITableViewCell, UINavigationControllerDelegate {
and my issues that arise can be seen here:
What classes do I need to import to fix my issue?
UITableViewCell has neither a storyboard nor navigationController property. UIViewController has both of these. They are not interchangeable, they are different types. If you want to access the properties of the cell's tableViewController, you will need to store a reference to the view controller in your cell or find another way to get to it.
Referencing the view controller from the table view
In your table view controller:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithReuseIdentifier("CellIdentifier", indexPath: indexPath)
cell.viewController = self
return cell
}
In your table view cell add a property to hold the view controller:
var viewController: UIViewController?
override func prepareForReuse() {
super.prepareForReuse()
viewController = nil
}