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
}
Related
This is the first time I'm working with delegate in swift. I've a table view with fixed three number of rows and every row has a seprate .xib for cell.
I'm adjusting the height of each cell based on the data I am getting in the cell. So as the tableView is loaded before and the cells are loaded after so I've defined a protocol in UITableViewCell class.
Above CalenderTimeTableViewCell class I've:
protocol ReloadingTable {
func updateTableView()
}
Inside this class above awakeFromNib() I've: var myDelegate: ReloadingTable? and in the method I'm getting data (using Alamofire) I'm calling self.myDelegate?.updateTableView().
In the viewController in which I've the tableView first of all I extended the class ReloadingTable and in this class I've:
func updateTableView() {
tableView.reloadData()
}
By applying break points the code did get to the self.myDelegate?.updateTableView() but in viewController updateTableView method is not being called.
As #vadian has commented you should assign target class to myDelegate
in cellForRowAt function assign target class to my delegate
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! CalenderTimeTableViewCell
cell.myDelegate = self
//Other
}
According to my other thread, I have a signal SIGABRT error in my code. The code consists of:
A Storyboard with 1 TableView and 1 Table Cell included
A custom Tablecell class (TableViewCell_Prototyp), which is connected to the Storyboard by (strong) IBoutlets.
A View Controller class, which is described in the following.
My ViewController class is connected to 3 super-classes: UIViewController, UITableViewDelegate, UITableViewDataSource. In addition, there are 3 functions implemented. Can somebody find the mistake ?
override func viewDidLoad() {
super.viewDidLoad()
}
i don't know what happens here. It was written by default so it should not cause any errors.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
i just want one table cell to be returned.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell (withIdentifier: "picCell") as! TableViewCell_Prototyp
cell.shoutPic.image = #imageLiteral(resourceName: "Bildschirmfoto 2017-06-05 um 10.11.21")
cell.shoutText.text = "Hallooooooooo"
return cell
}
shoutPic and shoutText is connected to the TableViewCell_Prototype custom class by outlets.
When you have custom table cell classes registered for your tableView, it's better to dequeue them using dequeueReusableCell(withIdentifier:for:), like so:
let cell = tableView.dequeueReusableCell(withIdentifier: "picCell", for: indexPath) as! TableViewCell_Prototyp
If this is all of your code, I would check that your prototype cell's identifier matches "picCell" exactly and that its class is set to TableViewCell_Prototyp
I think #Gereon is right that you need to dequeue with a different method.
Having the class TableViewCell_Prototyp is not enough -- you need to set it to the cell in the Storyboard file. Click on the cell in the storyboard and set it in the Identity tab.
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.
Simply put, I have a slide navigation view controller in my app with a back table VC and a front table VC. A selected cell in the back table VC is supposed to segue to the front table VC (embedded in a navigation VC). To illustrate, here are the isolated pre-change settings and code with it working properly:
Here is the simplified working code in my back table VC's cellForRowAtIndexPath method:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let navigationCell = tableView.dequeueReusableCellWithIdentifier("NavigationCell", forIndexPath: indexPath) as UITableViewCell
return navigationCell
}
Ok, onto the fun part. All I want is to replace the default UITableViewCell shown above with one of my custom cells in a Xib file. Now I'll illustrate what I modified. First, here's my Xib cell in my NavigationCell.xib file. Nothing special.
My NavigationCell class code is simply
import UIKit
class NavigationCell: UITableViewCell {
#IBOutlet var nameLabel: UILabel!
}
Finally, I modified my code in my back table VC to register the Xib and dequeue the custom cell:
override func viewDidLoad() {
super.viewDidLoad()
let navigationCellNib = UINib(nibName: "NavigationCell", bundle: nil)
tableView.registerNib(navigationCellNib, forCellReuseIdentifier: "NavigationCell")
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let navigationCell = tableView.dequeueReusableCellWithIdentifier("NavigationCell", forIndexPath: indexPath) as! NavigationCell
return navigationCell
}
All other code and specifications remain the same. The storyboard segue for the new custom cell is still from the class SWRevealViewControllerSeguePushController as shown in the first screenshot for this question. I simply swapped out the default UITableViewCell with my custom Xib cell.
This modification was enough to stop the segue from occurring. With this modification, after I build and run when I slide out the back table VC navigation menu and select one of my custom navigation cells (which display properly), it doesn't trigger any segue. Any thoughts?
I'm an idiot. I had to perform the segue programmatically in didSelectRowAtIndexPath.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("MySegue", sender: self)
}
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.