Add an action to a custom UITableViewCell - ios

I am building a simple app with a table view filled with custom view cells and using a storyboard. I want to add a actions on the cell each time the user tap it.
So far, I tried to create an IBOutle to link my cell to my tableViewController and add the action manually in the code but each time I try to do it I get an error message saying "Illegal Configuration - cannot have a prototype object as its destination".
The only quick fix I found is to add a UIButton with a transparent background and no title which fills in the whole cell and attached the action to it.
Is there any more elegant way to do it?

Not only is there a more "elegant" solution, but there is also a correct way to do this.You should be using the didSelectRowAtIndexPath method for your cells.
You should read what the Apple Docs have to say
And also, here is a tutorial on how to use row selection in your tableView.

The elegant solution is to use your table view's delegate method tableView:didSelectRowAtIndexPath: and put all your action code there.

Related

Only one cell could get user input - TableView

I have three cells in my tableview which is standard tableview not custom cell. However, I would like the last cell to be editable, in other words I want that cell to get user input, something like textfield, but other cells just static and not editable.
I know how to make custom cell and make it work, but before I dive into that option I would like to know is there a tweak that I could use.
I wonder it is possible without making all tableview in custom cell?
You can use UITableViewHeaderFooterView instead of last cell. I think it is better way to add some UI into TableView.
You just need to override a method which is here: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/index.html#//apple_ref/doc/uid/TP40006942-CH3-SW22
There is a simple example here:
http://www.ioscreator.com/tutorials/customizing-headers-footers-table-view-ios7
I hope that is helpful for you.

How to Use Custom UIButton in UITableViewCell?

I am trying to figure out the best way to apply UIButtons in tableViewCell, I've used tag method. like cell.customButton.tag = indexPath.row, then giving it a target.
But it din't work fine for me if the tableViewCell has may things to show with the same button in all the cells.
There are two options to show the title of the button , just like if we follow a user and unfollow, so after deleting or clicking on any button , it changes the title of some other button also , So, tag method isn't a good option I guess.. Any help would be appreciated.. Thanks!
Using a protocol in a custom table view cell class is what i would suggest:
check out this gitHub commit or download zip for the full project
Adding a Subclass and protocol to a UITableViewCell
I shall have this in the form of an answer later. let me know if you have any questions about the project

How do I get an event message from UITextField in a custom table cell to the table cell?

I'm sure this is a very noddy question, but I just can't get anything useful from the manuals.
I've a UITextField embedded in a custom UITableViewCell. If I use a delegate for the UITextView - it just gives me the UITextView.
If I link the event to the ParerentViewController, it just gives me the ID of the UITextView.
What I want to do is handle the event at the UITableViewCell level. Can I get the events from this text control to feed to the parent, and handle them at the parent level.
I don't seem to even be able to find the parent cell from the UITextField that the handler gives me. How do I find out which cell within the table the UITextField is in when I'm trying to process the events.
I have to be missing something obvious, as I've done this in earlier (pre storyboard) versions, and I'm sure it wasn't this difficult.
Answered a similar question the other day. See the accepted answer.
How can I get index path of cell on switch change event in section based table view
This will let you handle the text changes in the cell and pass them on to the table view. In your case its a UITextView and not a UISwitch.
Add properties to the cell for anything you will need inside the block code when it is called.
Create a subclass of UITableViewCell and implement the textField delegates inside the subclass. The code is lengthy but if you want I can post it when I get time.

How to create 'containers' similar to Facebook?

I'm creating an app that will get data from an RSS feed and I want to know how I recreate something similar to the Facebook app 'containers'
An example of what I mean is below.
How would I go about recreating the boxes where the statuses are held?
To achieve your desire result you have to create custom table view cell using UITableViewCell and arrange views as per your need by creating subclass of UITableViewCell
Just create a UITableView with different prototype cells and then deque the appropriate cell identifier in cellForRowAtIndexPath.
You probably have to subclass UITableView for achieving the desired effects such as indentation etc.
After that you have to make custom view for each cell and return them from the method cellForRowAtIndexPath

UITableView inside UITableViewCell - didSelectRowAtIndexPath

I am making an interface which have UITableView with four custom UITableViewCell's. And every other UITableViewCell have also UITableView. This mean I have TableView inside a TableView.
Let me call first tableView - ParentTableView and nested tableView - ChildTableView. So I implemented method didSelectRowAtIndexPath on both tableView's. But when the app is running, only the method of the ChildTableView is being called. I need to know inside the ParentTableView, which cell is being tapped.
How can I transfer that information further from ChildTableView to ParentTableView.
This may be a silly question, but I can not find any reliable solution so far, so please help me.
Thank You in advance, kind Sir
First, I think nested table views is a bad idea. But I don't know your use case, so it might be an exception.
The table view controller class used inside a cell have its own #protocol definition and set the outer table view as its delegate. In the inner didSelectRowAtIndexPath: it can inform the outer table view about the selected indexPath, its own indexPath and any other information you might want to transmit.
Try using collection views. You can do a layout that works like a regular table view and then in the cells that need to have a table, you can make those separate cells with another collection view inside or a table view inside of it.
As mentioned before, it's not easy doing table views inside of cells, and it can be very tricky to get things to work correctly.
This is an old tutorial I wrote which may help and be of guidance. But since then there's been collection views and auto layout, so keep in mind it's very old.

Resources