TableViewCell Listener with Swift - ios

Good Evening!
I have a specific question about Xcode (Swift) and the TabelView Layout.
My App has until now 1 View within a Container in this Container is a UITableViewController embeded which shows a nice static table view.
My mistake is how I can register when a User click on one of this table view cells?
The method:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//CODE TO BE RUN ON CELL TOUCH
}
doesnt work for me - the method is not called, an alert inside of this method is not shown.
Perhaps a false file structure? In my main storyboard I connected a Swift file with the UITableViewController which is inside of the container of the main view controller. Code of the File ist the basic:
import UIKit
class test: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
How can I register when a User click on a TableViewCell? For example showing an alert?
Please help me, I'm really new in iOS development.
Thanks!
Here are two pictures of my project-structure:
storyboard-structure
tableView-structure

A functioning table view requires three table view data source methods. make sure to add them .
func numberOfSectionsInTableView(tableView: UITableView) -> return Int number of section
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) ->return Int number of rows in section
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> return UITableViewCell that populate the cell data
hope it help !!

A really silly mistake... The code was absolutely correct - the problem was in the main.swift file, the file within the container.
Above the container I've added a searchBar and used the following code to hide the keyboard by clicking outside the searchbar: https://stackoverflow.com/a/27079103/3849220
This code crashed my touch Events in the whole View - also "above" the container... dismissKeyboard() was called before the tableView function.
#Paulw11
A big thank you for your example project! It helps me really much to understand the logic and searching the mistake. Thank you!
I replaced the code and write a little own script that hide the keyboard by clicking a "done" button. Now it works!
Thanks at all!

Related

Merging static table view with a UIViewController

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
}
}

Programmatically switch UITableView in UITableViewController

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.

UITableViewController as subview swift

I'm trying to make my first app and I'm stuck at this point.
My app looks like snapchat, a horizontal scrollview and the left part is composed of a tableview.
So, in this left part I put as a subview a tableViewController. The problem is the cells are always empty ! The table view is showing with the right cell's height but cells are empty.
I'm sure you can help me, thank you !
I make the subview here
Link with cells here
Please be ensure that override necessary methods in table view controller and balance calls in adding child view controller code rutin.One more last thing when adding child view controller you should provide correct size to controller view.
for adding table view controller as a child view controller
guard let list = R.storyboard.dashboard.addressesController()else {
return
}
self.list = list
self.addChildViewController(list)
self.scroll.addSubview(list.view)
list.didMoveToParentViewController(self)
list.tableView.layoutIfNeeded()
list.view.snp_makeConstraints(closure: {
(make) in
make.left.equalTo(self.view).offset(10)
make.right.equalTo(self.view).offset(-10)
make.bottom.equalTo(self.scroll)
make.height.equalTo(list.tableView.contentSize.height)
make.top.equalTo(authorizedTop.snp_bottom).offset(10)
})
for extending table view controller
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(self.cellIdentifier) as UITableViewCell
cell.textLabel?.text = self.tableData[indexPath.row]
return cell
}
I know this isn't an answer to you question, but I have a suggestion which might help you in the long run, and maybe help you debug your current problem:
How about splitting the tableViews' logic into separate UITableViewControllers? That way you avoid some gigantic common UIViewController. You can use the Container View in your storyboard, which can embed a UIViewController.

programmatically generate different static UITableviews

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
}

Static tableview cell stays highlighted when returning from different scene

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.

Resources