I found several questions similar to mine but maybe not related to the same problem.
I'm using swift for my code and I noticed a problem using tableView.
I want to implement cell selection using didSelectRowAtIndexPath.
That method works perfectly if you create a UITableViewController class from scratch.
On the contrary using UIViewController with a tableView using proper delegates UITableViewDataSource and UITableViewDelegate the same method does not work.
I don't know what am I'm doing wrong.
EDIT:
The cell selection is implemented as
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("selected")
}
adding the override in the UITableViewController class
my main ViewController has the following protocols
ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
// my code here ....
}
This code has no errors. Make sure you have connected delegate from the xib or storyboard or from code and check maybe you have edited in xib the selection property of tableView or tableViewCell
Related
For some reason, am unable to override willDisplayCell forItemAt despite conforming to protocol UICollectionViewDelegate.
Trying to implement a custom UICollectionFlowLayout. Need to be able to lazy load via api, by using this delegate method.
I took raywenderlich's final code - https://www.raywenderlich.com/4829472-uicollectionview-custom-layout-tutorial-pinterest for Pinterest layout, and on adding
override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
print("fetch")
}
method, fetch was printed every time CV was scrolled.
No error shown.
If I remove override keyword, then an error is shown - "Overriding declaration requires an 'override' keyword"
For the working copy, there is override keyword.
PS> This question is now solved, turns out RW was using UICollectionViewController while I used CollectionView on normal ViewController. Hence they had to use override while I didn't need to!
collectionView:willDisplayCell:forItemAtIndexPath: is a protocol func. So you do not have to override. Remove override.
First thing, you don't need to override here
Your class is just ViewController, a subclass of UIViewController unlike the reference you're giving PhotoStreamViewController is a subclass of UICollectionViewController
Make sure you're not confirming redundant delegate UICollectionViewDelegate, that is in the controller definition and
again while extending(screenshot you pasted)
I have tested the sample from the link you have given, and it's working perfectly.
Needless to say again, conform delegates once:
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, PinterestLayoutDelegate {
//all the required delegate methods
}
NOTE: I know this is a re-post; I posted the same question a few days ago sans-code and, understandably, it was closed. I edited the question to add some relevant snippets but the question was not reopened, so I am reposting here. If this isn't the proper way to do this, let me know!
I have an application with two modes, and the settings screen is slightly different for each mode (one additional section in one of them, a few differences in number of rows, different UserDefaults keys, etc.). In the past I implemented this with a crapton of switch and if statements, but in an effort to make things more maintainable I'm investigating ways on splitting the two modes into their own separate classes of something. Initially I considered making two separate UITableViewController subclasses, however I was having trouble thinking of how that would work with storyboards and such. I then thought to use two separate UITableView subclasses, and select which one to show based on the mode in viewDidLoad.
However, I'm having issues with this approach. I have it set up so that the controller's cellForRow method calls the TableView's cellForRow method, but that's where things break. When trying to do a dequeueReusableCell, the app crashes with the ever vague "EXC_BAD_INSTRUCTION" error on that line.
Here's some relevant code:
ViewController.swift
...
override func viewDidLoad()
{
super.viewDidLoad()
...
tableView = SRScaleSettingsTableView()
}
...
override func tableView(_ tableView: UITableView?, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
return (tableView?.cellForRow(at: indexPath))!
}
SRScaleSettingsTableView.swift
override func cellForRow(at indexPath: IndexPath) -> UITableViewCell?
{
...
switch indexPath.section
{
case 0:
...
let switchCell = dequeueReusableCell(withIdentifier: "SwitchCell") as! SRSwitchCell
^ Debugger breaks on that line with EXC_BAD_INSTRUCTION
...
return switchCell
...
}
}
Any ideas on what would cause this? Is my approach even correct; is there a better way to do this?
You can keep a single UITableView class (you likely don't need to subclass UITableView at all) and a single UIViewController subclass. Create two different classes that implement the UITableViewDataSource protocol (and possibly also UITableViewDelegate). These two classes can implement the various delegate/datasource methods (e.g. cellForRowAtIndexPath, numberOfRowsInSection, didSelectRow) in completely different ways for the different modes your app needs to run in.
protocol SettingsSource: UITableViewDelegate, UITableViewDataSource {
}
class SettingsSourceForModeA: NSObject, SettingsSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int)...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)...
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)...
}
class SettingsSourceForModeB: NSObject, SettingsSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int)...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)...
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)...
}
class SettingsViewController: UIViewController {
#IBOutlet tableView: UITableView!
var source: SettingsSource! {
didSet {
tableView.dataSource = source
tableView.delegate = source
}
}
override func viewDidLoad() {
super.viewDidLoad()
// whatever logic is appropriate to determine the mode
if appIsRunningInModeA() {
source = SettingsSourceForModeA()
} else {
source = SettingsSourceForModeB()
}
}
}
The key detail in the above code is the source variable in SettingsViewController - the value of source is based on which mode the app is running in, and it determines what class will be used as the data source for the table view.
The storyboard setup is simple: one scene SettingsViewController, and a single stock UITableView in that scene.
Note that the SettingsViewController above is a UIViewController subclass, not UITableViewController, since the data source and delegate protocols are being implemented in separate classes and determined at runtime. This will require you to manually wire up the tableView outlet in your storyboard. However, you do not wire up the UITableView's dataSource and delegate outlets in the storyboard. Instead, it's done at runtime as shown in the sample code above.
Note that you may not have any need to implement UITableViewDelegate, in which case you can just ignore the references to UITableViewDelegate and its methods in the sample code above. Or, if you the UITableViewDelegate implementation (such as the didSelectRow method) is identical for the two modes your app can run in, you may be able to implement that in your view controller class, in which case you can wire up the delegate outlet of your table view directly to your view controller, in the storyboard.
You have a misunderstanding of how the UITableView and UITableViewController work together. A UITableView needs a UITableViewDataSource to provide it the details of the underlying data (number of sections, number of rows and actual cells, etc). This is what the UITableViewController does (it conforms to the UITableViewDataSource). So if you call the cellForRow for the tableView then it will call it's data sources cellForRow method to obtain that.
So in your code when you do this:
return (tableView?.cellForRow(at: indexPath))!
Your table view calls its data source which is your UITableViewController and that calls the table view cellForRow and so on. You have just entered a recursive loop which is eventually killed off with the error you see.
As for your overall approach I would go down the two UITableViewControllers route as that separates out the different logics between the two making it easier to both understand and maintain and also allowing for more reuse as well.
As for how that works with storyboards it depends greatly on how you switch between the two modes but in essence you can setup segues to switch between the two controllers.
I am learning swift 2.0, and I was wondering if you still need to add the code tableView.datasource = self and tableView.delegate = self like in Obj-C to conform to the protocols?
Example code:
class AboutViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
{
override func viewDidLoad()
{
super.viewDidLoad()
// conform to protocols
aboutTableView.dataSource = self
aboutTableView.delegate = self
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 2
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return 50
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
// Code here
}
}
Now the table view loads with the correct data in each cell.
However, if I remove aboutTableView.datasource = self and aboutTableView.delegate = self from viewDidLoad, my table view is blank. Why is this?
Is this code still required because I see many youtube tutorials that does not include this anymore in swift, and I'm confused as to why mine doesn't work without it?
First of all, that is completely independent of which language
you use, Swift or Objective-C.
But there are two different cases which may cause the confusion:
A UITableViewController subclass:
UITableViewController already conforms to UITableViewDataSource and UITableViewDelegate. It has a tableView property,
whose dataSource and delegate property are already set to self.
In your subclass, you typically override the
data source and delegate methods.
A UIViewController subclass with a UITableView property:
Here you have defined a UITableView property in your subclass
and initialize it in your code, or
connect it to a table view in the interface builder.
In this case you have to set the dataSource and delegate
property of the tableview, either in code or in the interface
builder, as explained in luk2302's answer.
If data source and delegate are the view controller itself,
then you have to declare the protocol conformance explicitly,
and implement the
data source and delegate methods (but without overriding
a superclass method).
Of course, in both cases, the table view data source and the delegate
can be set to a different object, it does not have to be
the view controller itself.
Yes, some assignment is still required.
Either explicitly via code
OR
What you can do instead is connect them already in the interface builder, making the explicit assignment via code obsolete. That is probably what a lot of tutorials do.
I've just joined StackOverflow and i'm struggling with a programming requirement with a iPhone app I'm developing in swift. I have a tableview list of different calculators and i would like to segue to another UITableView when a item is clicked to then do the detailed calculations for that tool.
I am likely to have lots of tools in the first table (>20) and so i don't want to use storyboard to draw up each new UITableView static table with a different segue for each one.
I wonder if anyone can give me some advice on how to programmatically code a presentation of a new UITableViewController with static cells when a item is clicked. I don't want to use storyboard so i would need to use code to both manage the presentation as well as the generation of the next UITableViewController with static cells.
I have been able to program a static tableview with a custom class programmatically and linked to a storyboard UITableViewController item but i want to do all this programmatically and cut out storyboard all together.
Any advice you could offer would be greatly appreciated.
Thanks.
A UITableViewController abstracts some things. It seems like maybe what you want to do is to separate things out and have a little more granular control.
You can do this fairly easily. You need 3 things to make this happen:
UITableView
UITableViewDataSource
UITableViewDelegate
A UITableViewController puts these all together for you. We'll have to create them ourselves.
To do this, we make a View Controller, and inherit UITableViewDataSource and UITableViewDelegate
class Example: UIViewController {
}
// MARK - UITableViewDataSource
extension Example: UITableViewDataSource {
// We need to implement some methods here
}
// MARK - UITableViewDelegate
extension Example: UITableViewDelegate {
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Handle the user clicking an item here
}
}
Three things left to do:
Create and show the table
Get the data to display in the table
Implement the delegate methods
Creating the table
You should decide if you want to completely programatically create a UITableView, or have Interface Builder lay one out for you in a .xib, and you just link it up via an IBOutlet.
Assuming you want to do the former, you can do the following:
var table: UITableView?
override func viewDidLoad() {
super.viewDidLoad()
table = UITableView(frame: view.bounds)
view.addSubview(table!)
table?.delegate = self
table?.dataSource = self
}
Get the data
When you push this view controller from your previous view controller, be sure to set a variable on this view controller with your data. Assuming you have an array, it's as simple as something like:
exampleViewController.myData = someArray;
navigationController?.pushViewController(exampleViewController, animated: true)
(be sure to create the myData variable in your View Controller to take this)
Implement the delegate methods
Now we can implement the delegate methods to show the data. You may already be familiar this, but for the sake of completeness:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// You should really use dequeueReusableCellWithIdentifier here.
var cell = UITableViewCell()
cell.textLabel!.text = myData[indexPath.row]
return cell
}
I have a uiviewcontroller with a container on it, embedded inside of that is a uitableviewcontroller that has static cells and static content. When I tap the cell "Trip Info" it segues to the Trip Info View Controller. When coming back the static cell is highlighted.
I have seen many posts saying to add code to the didselectrow tableview method but I dont use one because my content is static. Any ideas?
Okay, just because you have static content doesn't mean you get to skip using the tableview delegate. It's not hard to set up.
tableView.deselectRowAtIndexPath(index)
There's not another way.
I ended up figuring it out on my own.
You need to make a swift file for that UITableViewController that is embedded into the UIViewController.
Then make sure your tableview is a delegate, and then call the
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
in that new file you created.