iOS/Swift - Tapping on tableview cell doesn't do anything - ios

My app is essentially a UITableView of UITableViews. But when I click on a cell, nothing happens for some reason. The cell just gets grayed out. I'm unsure why this happens. I've spent the last 2 hours trying to figure it out but I just can't find the problem.
My appologies if this turns out to be such a silly problem. I'm new to iOS development.
The full project can be found here: https://drive.google.com/open?id=0B1GgS8Pwx4SBZjg3YjduejBPemc
I reckon the files which are relevant are
Main.storyboard
MasterViewController.swift
IntermediateViewController.swift
A little description: The app is meant to save your favorite twitter search queries. Essentially, each tag maps to a certain query. When you click on a tag, you get a UITableView of tweets returned by searching twitter API with that query. I haven't yet defined what happens when you click on a tweet, but a double click brings up a UIAlertController so that you can reply to that tweet. A long press follows that user.
None of this is relevant, though. The problem is in the UITableView to UITableView transition thats triggered when you click on a cell.
Places of interest:
MasterViewController.swift:
prepareForSegue()
Callback methods
Main.Storyboard:
Segues and structure
Any help would be appreciated. I've stretched myself out trying to figure out what's the problem. Please describe the problem when you see it.
Thanks

You should perform a segue when a cell is selected. In your tableView controller in the following method:
func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
create and present the other view controller using :
self.presentViewController(_ viewControllerToPresent: UIViewController,
animated flag: Bool,
completion completion: (() -> Void)?)
And if you've created the segue in the storyboard, you should set the segue identifier in the Storyboard to "showDetail" as indicated in prepareForSegue in MasterViewController.swift

Related

UITableView didSelectRow not called

In an iOS app I have a ViewController that has a ContainerView in which a ViewController (say VC2) resides, using an Embed Segue. This VC2 owns a UITableView and it's datasource and delegate are set to the VC2 and also conforms to the UITableViewDataSource and UITableViewDelegate protocols.
Now the cells are rendered perfectly, so the dataSource seems good. But when I try to select a row, nothing happens, so there seems to be a problem with the delegate.
However, the delegate methods willDisplayCell and shouldHightlightRowAt do get called. And I only implemented these method to check whether the delegate is set up correctly. And no, I did NOT implement the didDeselectRowAt method, which is a common mistake.
In IB the tableview owns two dynamic prototype cells, the Selection property is set to Single Selection and the Style is set to Plain. Both the UITableViewCells have a Style set to Custom and Selection is set to Default.
Why doesn't the didSelectRowAt method get called? I did check the isEditing property, which is set to false; editing is not relevant in my case.
this is my didSelect method:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("didSelectRowAt (indexPath.row)")
}
I use Swift 4.
I'm sorry, but it seems that Xcode has given me a really hard time.
I just deleted the ViewController from the storyboard and built it up from scratch again. Also created a new file for the VC and now everything works just as expected. Sometimes the is the way to go is delete all your work and start over again and in this particular case... it was.
I really cannot say what could be the cause; but it seems that something got messed up in the storyboard file. At least, that is my best guess.
Wasted some time... Thanks anyway for attending...

Navigating from UITableView to another ViewController and then going back to the TableViewController causes some elements to disappear

I have a strange issue in my notes app.
I have a ViewController that is a UITableView delegate and datasource (I do not use UITableViewController)
The table view is a list of notes. Clicking one will open the editor view controller for the selected note.
When I click a cell, then click the "Back" button to go back to the table view, some elements of the table view UI appear missing.
Here is a before and after screenshot of the table view:
I am using Realm mobile database to store the data for the notes.
When I debug I see that data is actually populated in the cells, it just does not appear on the screen.
I tried removing the app and reinstalling it
I tried deleting the storyboard and rebuilding everything from scratch
I check the cellForRowAt indexPath and made sure the data is being populated.
Update - code for cellForRowAt indexPath::
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: NoteListCell.identifier, for: indexPath) as! NoteListCell
let object = results[indexPath.row]
cell.configureCellWith(note: object)
return cell
}
I even tried adding a background color to the labels that disappear to see if they are still there or is it just the text that's missing (it's not the text, the entire label was gone).
I don't have anything special in my code for handling the tableview data or navigating between view controllers. I have nothing in my viewWillAppear and viewWillDisappear methods for both VCs that causes this (i tired removing everything from those methods but the issue remains)
Anyone has any idea on what else I can check?
So I still don't know why this was happening, but I installed Xcode 9 beta and ran the project from there (I was using 8.3.3 before) and the problem disappeared.
It's possible that reinstalling 8.3.3 would also solve the issue.

iOS Swift: Swipe table cell to perform action (not reveal)

Recently I've been using Google's Inbox app, and I found the swipe to mark done/snooze feature very concise and useful. Since I have an app with a similar tableview interface in the works, I've been looking around for ways to implement this in iOS, but I haven't found anything. The closest I've gotten is swiping to reveal extra actions (as seen in the iOS Mail app), but that entails having to both swipe and click on a button, which is rather unwieldy. Is there a way to bundle an action into the swipe itself, instead of having it reveal some buttons?
You may be able to achieve that by implementing the method
func tableView(_ tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool
in your delegate, respond with false and perform the feature when the function is called

UITableView didSelectRowAtIndexPath doesn't get click

I use for my app UITableview and I got a problem.
Method tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) called only by sliding finger from left to right or from right to left the item. But when I do normal click by this item, this method doesn't called
What is supposed to do the cell when you click on it?. As a first impression, the line tableView.deselectRowAtIndexPath(indexPath, animated: false) is not animating the select of the cell, and that can cause the impression that nothing is happening.
Second, the implementation of the cellForRowAtIndexPath: and didSelectRowAtIndexPath: methods look almost the same. I mean, in both methods your are getting the same data from the categories array and not changing anything, at least that's what I can see with the code you provided.
Third, may the implementation of the code inside your subclass, to which item belongs to in let item = categories[indexPath.row], be wrong.
Fourth, why your code have semicolons?.
You can review all of these to start discarding.

Swift coding multiple tableviews- how to connect them up?

Hi I'm new to coding so basically i've created a tableview app on github
the first one shows what i want to do without code https://github.com/edlen/guideline.git
and the second one is an attempt to recreate it with code
https://github.com/edlen/guidelines-via-code.git
but I'm really stuck about how to create code for going between screens so say for example on the main menu I select helpful info, how do i make the tableview with those options appear? and then if i select PO switch how do I segue to it?
Technically I can do what I want grand without code but I would really like to be able to understand coding of tableviews better and how to go between multiple tableviews and then select a final individualised view controller full of text.
Any advice that you could offer would be great- but please be prepared to speak very slowly and clearly as I'm still learning as i go along!!
At first, designing the app with Interface Builder inside Xcode is both the same for Swift and Objective C.
A UITableViewController should be embedded inside a UINavigationController, if you want to switch between views. Just drag'n'drop a UINavigationController and a UITableViewController into your storyboard - make sure the UITableViewController is the root viewcontroller of the navigation controller (right-click drag the tableview controller on the navigation controller).
After that, create a new Cocoa Touch Class - file (or press CMD-N). Look at this:
Make sure you choose UITableViewController at "Subclass of".
After that, go back to your interface builder and select your UITableViewController. There go to the identity inspector to change the class of your tableview controller. Look here:
After that, you just need to choose a reuse identifier for you tableview cells. You need the reuse identifier, so that your iPhone (or iPad) can reuse a cell and doesn't have to create a new one everytime. I just chose "myCell" as reuse identifier. To do that, click on the cell and go to the attributes inspector:
OK, the part with interface builder is done. Now go to your previously created MyTableViewController.swift.
There you have some methods. You need at least these to methods:
func numberOfSectionsInTableView(tableView: UITableView) -> Int
and
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
You need these methods, because the data source of your tableview is set to self. That means, this class is used to fill your tableview with information.
Let's start simple. To test everything, just use 1 section. So in your numberOfSectionsInTableView return 1.
If you have some data to be displayed, I think you'll store this data inside an array? However, in numberOfRowsInSection you have to return the number of rows that have to be displayed inside that section. So if you want to display 3 rows, just return 3 here. That makes 1 section with 3 rows inside that section.
Now you have to customize your tableview cell with labels and maybe an image view. So you need the function
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell
// Configure the cell...
// add some UILabels or UIImageViews here.
return cell
}
to create your own cells. Here you'll need the reuse identifier you've set in interface builder.
A UITableView also has a delegate, which is responsible for events like (de)selecting a cell. So if you want to show a UIViewController when the user taps on a cell, you have to implement the method
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
Here you can switch between views. Here you need to have the tableview controller embedded inside a navigation controller (except you are presenting your views as modal view controllers, but that's not the point here).
So the user taps on a cell, you have the index of the cell inside indexPath.row - you'll need that to show the user the information inside the new view. After that, create your UIViewController or whatever, and present the viewcontroller with
self.navigationController?.presentViewController(myViewControllerToPresent, animated: true, completion: { () -> Void in
// some code to do after the presentatioin is ready
})
That's it!

Resources