Simple table view in iOS8 with swift and storyboard - ios

I am trying to get used to swift language so as usual after learning some basics about it I have started with storyboard and table view.I have dragged table view controller to storyboard
and created class for it and attached to view controller also.
As soon as I uncomment cellForRowAtIndexPath method I get following swift compiler error
Overriding method with selector 'tableView:cellForRowAtIndexPath:' has incompatible type '(UITableView!, NSIndexPath!) -> UITableViewCell!'
I also have included numberOfSectionInTableView and numberOfRowInSection delegate methods.
I don't know what I am doing wrong here.
so can anybody please help me resolve this issue.
Thanks in advance.

replace your function with this :
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
}
This will solve your problem.
EDIT:
If you want to use existing function then you can remove all ! from function and your error will gone.

Related

I don't see my buttons when running the simulator - Xcode10

I was trying to follow a tutorial online but the version that was being used was older than the most recent Xcode10.
I have added a navigation controller and added buttons to the controller...
navigation controller with buttons
But when I run the simulator, I do not see the 2 buttons that I created....simulator without buttons
Any ideas why this would be the case? All help is appreciated :)
To calculate the height, UITableViewCell rely on the content of the cell and to be able to do that you should add constraints to the buttons you have already added.
or you should implement the delegate method
func tableView(_ tableView: UITableView,
heightForRowAt indexPath: IndexPath) -> CGFloat

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

iOS - Configuring Custom UITableViewCell on VIPER pattern

I have been following VIPER design pattern for my current iOS (Swift) project and i am new on using design pattern on code. I am having trouble to take decision about customizing or configuring a subclass of UITableViewCell. Do i change the cell's on ViewController's ->
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell this delegate method or on my derived UITableViewCell's class instance method's ?? Actually the work is changing cell's IBOutlets like Label, ImageView from Model data
I think you should change your cell inside cell because you're not breaking the encapsulation and can make your IBOutlets private.
Actually you can incapsulate you UITableViewDelegate and UITableViewDataSource realisation in some object. We're creating TableViewAdapter for this. You can find example of TableViewAdapter in my open source VIPER example here.

cellForRowAtIndexPath is called but without stacktrace

Using Swift 3, the UITableView's func tableView(_ tableView: UITableView, cellForRowAt indexPath: #autoclosure #escaping IndexPath) -> UITableViewCell is being called. I want to track who's calling this function. As far as I know, this will only be called if there's a need for a cell to display. However, in stacktrace override internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: #autoclosure #escaping Int) -> Int is not called and there is no stacktrace. Could someone advice to where to start digging up? Thanks!
See attached screenshot
By default, you only see the stack trace that contains debug symbols. So you won't see many framework internals here.
To see everything, there is a small button at the bottom of the stack trace window, third position from the right, that will enable also methods without debug symbols:
(In fact, it depends on your Xcode version; in older versions there is some sort of slider which will display more or less details in the stack trace).
As #AndreasOetjen mentioned, this is a normal behavior because the method get's called from a UIKit Framework class. The call-stack in this Framework is hidden by default and can be opt-io by the mentioned buttons.

CellForRowAtIndexPath not working

I have a settings page and I am unsure as to how to make the Log out work. I have been told to use CellForRowAtIndexPath to make it so when I click "Log out" I can set it to actually logout using PFUser.logout()
Im sure this is a simple answer but I could not find anything remotely close to what I was looking for, does anybody have a quick solution? Thank you so much in advance.
You need to use the didSelectRowAtIndexPath method in the UITableViewDelegate protocol that tells the delegate that the specified row is now selected to handle the cell selected in the UITableView like in this way:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// call to the PFUser.logout() here to log out the user properly
print(indexPath.row)
}
I hope this help you.

Resources