What method needs implemented to get an action from the UITableViewCell click / touch? - objective-c

I'm new to the UITableView and noticed that w/out implementing a method the UITableViewCell simply turns blue when it's touched. What method would I need to add to get some action from this touch directly?
For example, when the cell is clicked I want to pass off the index to another method that will in turn do the same action that is completed by the accessory button click event.
Thank you in advance

You need to implement didSelectRowAtIndexPath method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// indexPath.row tells you index of row tapped
// here do whatever your logic
}

didSelectRowAtIndexPath
Link here

You need to assign a UITableViewDelegate to the table view, and implement one or more of the following 2 methods in the delegate class, depending on when you want the event to be fired:
-tableView:willSelectRowAtIndexPath:
-tableView:didSelectRowAtIndexPath:

Related

Multiple delegates for a table view

Hi I've been googling and I can't find an answer and maybe there's a different way to do this So I'm putting it to the community.
I have a tableview in a UIViewController. The UIViewController is the datasource and delegate for the table view. I then have a second controller which reacts to scrolling in the main UIViewController. Ideally I'd want the second controller to also be a delegate so that scrollviewDidBeginScrolling will fire in both controllers. I want to do this because it makes controller 2 very easy to implement because you'd set it as the delegate and pass in the tableview reference and it would do all the heavy lifting.
Basically can you pass an array of delegates to tableView.delegate? I could see a few situations where you'd want multiple controllers or views to react to an event like scrollViewDidBeginScrolling. Is there any way to accomplish something similar without having to do stuff like
-(void)scrollViewDidBeginScrolling:(UIScrollView *)scrollView{
[anotherViewController scrollviewDidScroll:scrollview];
[otherView scrollViewDidScroll:scrollview];
}
I'm using it for a controller than handles Pull To Refresh for tableviews and I want to make implementation as easy as possible with as few lines/methods in the tableview controller as possible.
You can accomplish what you need but you will need to chain the delegates. The table view will only have one delegate but you can define your own custom delegate method in the view controller that is the delegate of the tableview and have your other view controller set itself to be the delegate of that. Another brute force method would be to just send out notifications and have the view controllers register for the notification.
In you UIViewController1 delegate methods, just send the same call to UIViewController2 delegate methods. E.I.:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[delegate2 tableView:tableView didSelectRowAtIndexPath:indexPath];
// etc...
Edit for comment below. You could subclass UITableView and give it multiple delegate properties or an array of delegates property.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
for(id delegate in _delegates)
[delegate tableView:tableView didSelectRowAtIndexPath:indexPath];
// Do nothing else, because the delegates handle everything.
}
What you need is delegate multiplexing. I wrote a class to do just this: https://github.com/aleph7/MultiDelegate

How To Implement Tap a UITableViewCell In a Section Navigating To Multiple UITableViewCells For Details?

I'm now learning UITableView class on iOS development,I want to implement Tap a UITableViewCell In a Section Navigating To Multiple UITableViewCells that accessory type for checkmark,What should i do?Please tell me how to do for details,Big Thanks for your help!
Like this picture:http://i.stack.imgur.com/kowdl.jpg
You need to implement UITableView delegate method-
- (void)tableView:(UITableView *)tView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Now to identify specific UITableViewCell you can use indexPath - indexPath.row and indexPath.section.
In your current view controller, you should implement didSelectRowAtIndexPath, you will need to create another UITableViewController, and when a cell is selected in your didSelectRowAtIndexPath, you will push your new UITableViewController.

Can I add a disclosure Button to only one cell in my UITableView?

I have a UITableView and I want to add a disclosure button to a cell but only when the cell is selected.
How can i do it?
Thanks
in tableView:didSelectRowAtIndexPath: save the indexPath to a member variable.
in tableview:cellForRowAtIndexPath: check, if the indexPath is the saved one and set cell.accessoryType
Do it in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//bla bla bla
}
Remember to have a variable to remove the button from the previous selected cell
In the table view cell set the accessoryType or accessoryView property. You will want to do this in the -tableView:didSelectRowAtIndexPath: delegate method.
Doing it in didSelect means that the disclosureButton only appears after the user releases the selected cell. If you want ti to appear when the cell is highlighted, you need to subclass UITableViewCell and override setSelected: or setHighlighted:
However, the whole point of a disclosureButton is that the button can be pressed and does something different then simply selecting the cell. For that there is the disclosure indicator. The whole premise of making either of these show up only when the cell is highlighted/selected doesn't seem to serve any good UI design purpose in my opinion...but maybe for what you want it does make sense.

Selecting UITableViewCell programmatically?

I want to make a now playing button for my radio application. So would like to know how can I select a row programmatically I thought of calling didSelectRowAtIndexPath method but apparently we can't call it.
didSelectRowAtIndexPath is the method from delegate and it's called when you touch (select) the row in UITableView
Use below method to selct a row on your UITableView instance.
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
See Sample code.
[myTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
There is tableView method like [tableView selectRowAtIndexPath:anIndexPathObject animated:BOOL-Flag]; .By calling this method you can select the row programmatically. But you also need to create an index path object.

How to make a custom tableView cell accessory

I have not yet found any really good examples on how to do this. There is an image that I want to use as the accessory button and when I put it in and click on it doesn't work. So it looks correct but doesn't work...
Here is my code:
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
cell.accessoryView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"TableView_Green_Disclosure.png"]];
So how do I get my UIImageView to call accessoryButtonTappedForRowWithIndexPath whenever it is tapped?
A thorough reading of accessoryView and accessoryType would reveal that they are mutually exclusive ways to customize a cell.
Setting the accessoryType will cause the table view delegate method to be called when it is tapped.
Setting the accessoryView will ignore the setting of accessoryType and give you something to display. If you want to receive a callback from the custom view you've put in place, it should be a control that is wired up to do so. (Or any view with a gesture recognizer.)
If you use a button, and set its action to accessoryTapped:, you will receive the button as the "sender" argument. You can walk up the view hierarchy until you find a table view cell, and then ask your table view what the indexPath of that cell is. This will then get you an index into your model objects and you be able to act on it appropriately.
Alternate to the button, you can enable interaction on the UIImageView above, and add a gesture recognizer to it.
To make the button actually do something, you'll need to implement - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath from UITableViewDelegate.
When an accessory button is tapped in a row, this method will be called and you'll have the chance to act appropriately using the passed in index path to determine which row's accessory was tapped.
Check the blog post hdr->cmdline for creating custom accessory view for UITableView.
The author used UIButton objects with images for custom accessory view.
To make use of the accessoryView - you would need to set the cell's accessoryType to UITableViewCellAccessoryNone deposit a UIButton (with associated image) into the cell and then wire it up to receive user touches. You might use something like the code below as the IBAction response to the cell's UIButton being touched:
- (IBAction) accessoryButtonPressed:(id) sender
{
NSUInteger pathInts[] = { 0,0 };
pathInts[1] = self.currentselectedrow; // ivar set when tableview row last selected
NSIndexPath* indexPath = [NSIndexPath indexPathWithIndexes:pathInts length:2];
[self tableView:mytableview accessoryButtonTappedForRowWithIndexPath:indexPath];
}
The UIButton would be wired to execute this glue code by way of a line inside your tableview's "cellForRowAtIndexPath:" function
[thecell setButtonTarget:self action:#selector(accessoryButtonPressed:)];
One thing I noticed is that the UIButton seems to want a 'swipe right' versus a simple 'tap' touch in order to trigger the event - but it could be my beta iOS that's the problem. Note that I had added a UIButton* object named 'cell_accessoryButton' to the Custom Cell source.
In the cell's source you'd support the 'setButtonTarget' call with code like this:
- (void) setButtonTarget:(MyViewController*)inTarget action:(SEL) inAction
{
[self.cell_accessoryButton addTarget: inTarget
action: (SEL) inAction
forControlEvents: UIControlEventTouchUpInside];
}
It's so much easier to just use the accessoryType reference and let iOS do the heavy lifting - but, if you want a custom graphic, etc - this is another path that works.