UIbutton in UItableviewcell of UItableview help? - objective-c

I have searched stackoverflow to find an answer but i can not find one.
I have an uibutton in an uitableviewcell of an uitableview.
I tap on it. When i tap on it a function is triggered. Inside the function i am trying to get the indexpath of the uitableviewcell. The indexpath should be the one where the button i pressed previously exists. The function is triggered but i get a nil value when i access indexpath.
this is the code i use
NSIndexPath *indexPath = [walltablefriends indexPathForCell:(UITableViewCell*)[[sender superview] superview]];
but it is not working it is giving me 'nil' . Any help appreciated!

Your assumption that the buttons superview is a UITableViewCell is a dangerous thing, for instance if you add the button to the content view property of the cell then it's superview is a UIView not the cell view.
I would subclass my tableview cell and make that the target for the button press, and have a delegate for the cell which tells your view controller that the cell action was selected, something like this...
- (void)tableCellActionSelected:(MyCellClass *)aCell;
The cell can then call...
[cellDelegate tableCellActionSelected:self];

The table was empty as dreylin mentioned in a comment! Thanks man!

Related

iOS animate partial cells to show full cell

I've got an UITableViewCell with a UITextField in it. When I click the UITextField I want to make sure the entire cell is visible in the tableView. I did this in textfieldDidBeginEditing, like so:
- (void) textFieldDidBeginEditing:(id)textField {
TTTableViewCell *cell = (TTTableViewCell *)[self.tableView cellForRowAtIndexPath:objc_getAssociatedObject(textField, kIndexPathId)];
[self.tableView setContentOffset:CGPointMake(cell.frame.origin.x, cell.frame.origin.y - self.tableView.sectionHeaderHeight) animated:YES];
}
This works perfectly when the user goes down the list. Expect when to user taps a cell which is partial visible on the top it doesn't reveal the entire cell just moves a bit.
Does someone know a solution to this problem?
There are UITableView methods to solve exactly this. You can use scrollToRowAtIndexPath:atScrollPosition:animated: to ask the table view to bring a cell to the top by passing UITableViewScrollPositionTop.
(You might first want to check if it's already visible to ensure it doesn't scroll unnecessarily.)

How can I figure out the indexPath of a cell I'm not pushing using didSelectRowAtIndexPath?

I've got a UITableView with custom cells inside, which trigger a detail view when a UIButton in them is tapped. Tapping the cell itself does not trigger the segue, meaning didSelectRowAtIndexPath is never called.
Before implementing this, I was using didSelectRowAtIndexPath to set up the object being passed to the detail view via its indexPath in my NSFetchedResultsController's fetchedObjects array. Since I'm now not using didSelectRowAtIndexPath, I can't access the indexPath there.
I've already tried using the tag of the button, but since it can't store an indexPath, this doesn't work when I call objectAtIndexPath before the segue. I've also tried accessing the button's superview to get its indexPath, but that didn't work either, was always nil.
How else can I know which UIButton was pushed, so that I can pass the correct object to my detail controller?
Edit: I thought of the idea of adding a UIButton category that adds an indexPath property, but I'm having trouble doing this since it expects a method to go along with it. Would this approach work?
Follow the superview of the button (or the superview of superview of the button) to get the Cell and get the indexpath of the cell from tableview
I fixed by adding the following to the method called when I tap the button:
UIView *contentView = (UIView *)[sender superview];
UITableViewCell *cell = (UITableViewCell *)[contentView superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell: cell];
_quote = [self.fetchedResultsController objectAtIndexPath: indexPath];
[self performSegueWithIdentifier: #"ViewDetail" sender: nil];

Show UITableViewCell which can then expand to show more data

I have a UITableViewCell which is currently cluttered and has too much information on the screen at once. I want to make a button, that when pushed, a small view will slide down and show the remaining information, and then when pushed again it will slide back to the original size. I was wondering how I go about doing something like this. I know NOTHING about doing this, so please be specific when pointing me in a certain direction. Thanks!
Do you want single Button for all cells in table view or you want to add button as subview on each cell
1.if you want to add button as subview on each cell
a) Add button with tag value equals to indexPath.row and set target to single method for each button.
ex
-(void)infoButonTapped:(UIButton *)sender;
Now on Click on button find the tag value of button and get the info from the array which you use to populate UITableViewCells.
b) Now create a infoview and add textview on it and set info as text prop of text view and add infoview as subview of view using UIView Animation and hide on next click
you can either use a bool variable in .h file to know hide or show infoView or you can check if infoview has superview then you have to hide infoview else add infoview as subview of self.view.
if you want a common button for all Cell
a) Now on click of button get indexPath for selected cell of tableview using tableview method
- (NSIndexPath *)indexPathForSelectedRow;
this method either return indexpath for selected cell or nil in case no cell selected
Use indexpath for get info from the array and add infoview as subview to view using step 1.b explained above.
you can also get the rect of selected row for setting the cordinate for the info view
by method of tableview
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;
This may help you Show hide table
Or you can use BOOL as a property of cell and hide the subviews of UITableViewCell according to bool also resize frame of cell.
In this case, you better use the following method: -
(void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation{
}
Make a call to above method in your UITableView didselectRowAtIndexPath method.
More explanation is given in below link
UITableViewCell expand and collapse

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.

How can we know which cell is touched in tablView when a custom button in the cell is touched?

I need to know which cell is touched when any button inside the cell is touched. I have custom UIButton *button1 in the cell and UIButton *button2 on the cell.imageView.image of the cell. I wrote selectors for both buttons.; But, I could not differentiate the buttons for each cell. How do I know which cell button was touched. What to do, to know that a particular cell's button was touched ?
Thank you.
There are several approaches. If you create the cells programatically, you do something like set the buttons tag to the row number of the cell. Or you could have the action method query its sender (your button) for superview to location the cell.
This will get the work done!
-(IBAction)myButton:(id) sender {
UIView *senderButton = (UIView*) sender;
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[[senderButton superview] superview]]
}
Then you can use the indexPath to get the row and section number respectively.