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
}
Related
I'm currently trying to build a screen that contains 2 UIButton, and 1 UIImageView.
Below these UI Elements, I want to add a static table view that would contain 1 UITextField for each cell in order to create a kind of scrollable form.
The error I'm having is the following one:
Static table views are only valid when embedded in UITableViewController instances
While it doesn't seem possible to create a static table view without a table view controller, I was wondering if there could be any way to get the same result as my initial idea?
Please note that I'm building my UI using storyboard.
Here's a screenshot of what I was trying to build initially:
EDIT: I finally decided to use a static view controller, and implemented the buttons in a cell and the other textfields in different cells. Thank you all for your help.
You can add the UITableViewController as a childViewController to your bigger UIViewController (parentVC)
Then manage parentVC's view hierarchy so that you can achieve the 2 UIButton, 1 UIImageView and a table view at the bottom
In Xcode 10.2 you can use Container View to implement the UI you described. Drag and drop a container view object to the required view controller in your storyboard scene:
Then add UITableViewController instance to your storyboard scene:
Set Static Cells for it's Content:
Then right-click on Content View that you added in one of the previous steps, and setup it as described on the following screenshots:
Setup constrains and cells content. Then you will see something like that on your testing device:
I think you should manage this adding elements in a UIScrollView, there's no need to use a UITableView. So you can scroll all the contents when you show the keyboard
A static tableview is nothing more than a UITableViewController handling the UITableView's UITableViewDataSource methods on your behalf.
You can simply add a UITableView to your UIViewController, set the UITableView datasource to your UIViewController and implement the methods as appropriate.
e.g.
class MyViewController: UIViewController, UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
tableview.datasource = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// create UITableViewCell
}
}
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'm working on an app right now that requires the exact same table view in multiple scenes on my app, however, the data and the placement of the table will change, as illustrated in these two pictures (table view highlighted in red)
In all instances of the table throughout the app, it should have:
The same delegate methods
The same datasource methods (all though the actual data will change)
The same table view cell types with the same outlets and actions.
The same internal constraints (I don't want to have to manually add constraints for the table cells each and every time).
Right now I'm coding them as two separate table views in two separate view controllers, however, I've realized I will need to replicate the same table and logic in many more places throughout my app and this doesn't feel like the right way to go about. So my question is, how can you accomplish table replication in iOS (using interface builder or swift) that meets the specs above, in a clean and dry way?
If you want to design your cells once and then use them across different table views in different view controllers you have to design them in the separate .xib files. Prototype cells are per view controller and don't scale very well.
If all the data source and delegate methods are the same, you can move implementation of this protocols to the separate class. You can configure this class with the array of items to display.
class ReusableTableView: NSObject, UITableViewDataSource, UITableViewDelegate
{
var tableView: UITableView
var tableViewData: [String]
init(_ tv: UITableView, _ data: [String])
{
tableViewData = data
tableView = tv
super.init()
tableView.delegate = self
tableView.dataSource = self
// Register all of your cells
tableView.register(UINib(nibName: "SomeNib", bundle: nil), forCellReuseIdentifier: "example-id")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableViewData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return tableView.dequeueReusableCell(withIdentifier: "example-id", for: indexPath)
}
}
Having this two building blocks you can layout the table view separately on each view controller and wire it up with your reusable data source/delegate.
class ExampleTablewViewController: UIViewController
{
#IBOutlet weak var tableView: UITableView!
var reusableTableView: ReusableTableView!
override func viewDidLoad() {
super.viewDidLoad()
reusableTableView = ReusableTableView(tableView, ["lorem", "ipsum", "dolor"])
reusableTableView.tableView.reloadData()
}
}
You can find a basic example of how I see this in this GitHub repo.
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 have an existing ViewController + xib in my project, and now I want to add a UITableView with static cells, like this:
But when I drag a UITableView onto my screen I don't have the "Content > Static" menu in the Attributes Inspector.
I've tried making my controller subclass UITableViewController, but that doesn't help -- I still don't get the option to use static cells in Attributes Inspector.
I've looked around on StackOverflow but haven't found any existing answers to this question. All the existing questions relate to Storyboards (which I'm not using) instead of xib files.
My guess is that Xcode does some kind of magic when you add a UITableViewController to a storyboard, but not when you change an existing xib to inherit from UITableViewController.
Any advice how how to add a table view with static cells to an existing xib?
Static table view cells are only available when using storyboards. However, if you aren't using storyboards for your entire UI you can still use them for individual screens instead of a collection of screens.
To do this you can create a UIStoryboard file with a single view controller on it that has it's File's Owner set to your custom view controller subclass. Set the VC's identifier to some value. When you want to display this, get the storyboard and then instantiate your view controller subclass by creating the VC from your storyboard.
UIStoryboard *tableViewStoryboard = [UIStoryboard storyboardWithName:#"your storyboard" bundle:nil];
CustomViewController = [tableViewStoryboard instantiateViewControllerWithIdentifier:#"custom identifier"];
You can present this VC as usual in your app.
This is a great way to start using storyboards without having to convert your entire app to use them.
This is doable without storyboards:
your controller must adopt protocols for tableview delegate and
source...UITableViewDelegate, UITableViewDataSource
Add a table view to your .xib; set the table view's style to "grouped"
Connect the table to a property in your controller (let's call it "yourTableView")
Add table view cells to the .xib as separate views...i.e. not as subviews of the table view...they'll be free floating in the .xib. (see example image below)
Add controls / labels etc to the table cells.
Assign unique strings to the "Identifier" field in Attributes for each table view cell.
Connect the table cells to other properties in the controller (sliderCell, switchCell, etc)
Connect the send events for the cells' controls to IBAction-ed methods in the controller
- (IBAction) handleSliderCell, etc.
In the viewDidLoad method of your controller, assign the delegate and source of the table view to the controller (self.yourTableControl.delegate = self; self.yourTableControl.dataSource = self;)
implement numberOfSectionsInTableView, numberOfRowsInSection, and cellForRowAtIndexPath in the controller
in tableView:cellForRowAtIndexPath:, return yourFirstCell, yourSecondCell, etc pointers for appropriate indexPath values
Events for controls for the table cells should go to your handler routines...
Example for step 4:
To expand on BVB's answer, when it comes to number 10, here are the Swift 3 snippets for your tableview delegate methods. Be sure to add outlets to all your cells in the xib.
When you manually create your table view sections, remember that indexPath is equal to a 2 dimensional array representing your table structure. for example, when indexPath is passed to tableView(cellForRowAt indexPath: IndexPath) is equal to [1][0], your cell will be placed in the first position of the second section.
Values can be pulled out of indexPath with the properties indexPath.row and indexPath.section. Using these values, you can manually construct your sections from your IBOutlets in whichever order you prefer.
#IBOutlet var is_enabled: UITableViewCell!
#IBOutlet var is_public: UITableViewCell!
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch(section){
case 0:
return "Cell 1"
case 1:
return "Cell 2"
default: return ""
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("index path here", indexPath)
if(indexPath.section == 0) {
return is_enabled
}
else if(indexPath.section == 1) {
return is_public
} else {
return is_enabled
}
}