UIButton not working in tableview cell - iphone

I've added a button to a tableview cell, but I'm having two problems with it:
1) I've invoked a setTarget:action: method on the button, but the action: method never gets called:
[playButton addTarget:self action:#selector(playButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
2) the cell itself ends up getting selected on the touch, invoking didSelectRowAtIndexPath, which I don't want to happen.
I've verified that the button is at least seeing the touch by setting its showsTouchWhenHighlighted property and seeing it "glow" when pressed. But for whatever reason it looks like the touch event is then getting passed up to its cell superview instead of being handled by the button itself.
I've tried adding the button directly to the cell's contentView, as well as to other cell subviews, with no effect. I've also made sure that the button's superview(s) were all touch-enabled.
Any thoughts?
Howard

There are two ways to do that, the simplest is to add the button to the cell with IB, set a tag number to the button and when you are in the cellForRowAtIndexPath method updating the cell use something like:
UIButton *myButton = (UIButton *)[cellYouAreUpdating viewWithTag:-1969];
being -1969 the tag number you previously set in IB and cellYouAreUpdating the cell reference. After that you will have a reference to the button in the cell and you could set the add target with the proper selector.
Hope this helps.

My be your button Y axis OR Y axis + height is Higher than its super View (Super view may be cell or contentview of cell) check the heght of superview

-->For your first problem,use this code
UIButton *imgV1 = [UIButton buttonWithType:UIButtonTypeCustom];
[imgV1 setFrame:CGRectMake(0, 0, 320, 53)];
[imgV1 setBackgroundColor:[UIColor clearColor]];
[imgV1 setImage:[UIImage imageNamed:#"expired.png"] forState:UIControlStateNormal];
[imgV1 addTarget:self action:#selector(show_product_details:) forControlEvents:UIControlEventTouchUpInside];
-->For your second problem use this in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}

Related

Prevent UITableViewCell highlighting when image in content view is touched

I have added a UIImageView to the right side of the content view of my UITableViewCell.
I am trying to prevent the cell from being highlighted while the user is touching the UIImageView (because an attached gesture recognizer performs some other functionality).
I have thought about implementing tableView:shouldHighlightRowAtIndexPath: and returning NO, but I am unsure how to easily determine if the image is being touched when this method is called. Is there an easy way to do this?
Try this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
// Add tap gesture recogniser to cell's image view
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped)];
tap.cancelsTouchesInView = YES;
cell.theImageView.userInteractionEnabled = YES;
[cell.theImageView addGestureRecognizer:tap];
return cell;
}
The important parts are to make sure the image view has userInteractionEnabled = YES and the gesture recogniser has cancelsTouchesInView = YES.
This will cause the tapped method to fire if the image view is tapped, but the cell will not be highlighted.
why don't you add a UIButton with type custom.Will have the tap button action.Will not affect the cell highlight
Prevent highlight of tablecell
cell.selectionStyle = UITableViewCellSelectionStyleNone;
or
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

added button to uitableviewcell not responding

I have created a custom tableviewcell through interface builder with a label and a button. In the .h and .m file I have made outlets and actions for the button which I have connected.
This cell is added to a tableview controlled by a uiviewcontroller class. However my problem is that when I tap the button, the button is not activated, however I am pushed on to the detailed view belonging to the cell. It seems like the button is behind the cell.
Any suggestions for what I have forgotten or should change?
I have created a button programatically and added as subview to the cell instead and this works, however this gives me another problem as the button is added everytime a cell is loaded.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(200.0f, 5.0f, 100.0f, 40.0f)];
//Set image
UIImage *img = [UIImage imageNamed:#"myPackage.png"];
[button setImage:img forState:UIControlStateNormal];
[img release];
[button addTarget:self action:#selector(myPackagePushed:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
I would really like to use the interface builder approach - any suggestions?
Regards
try
[cell.contentView addSubview:button];
also
UIImage *img = [UIImage imageNamed:#"myPackage.png"]; here you are not allocating memory so no need for
[img release]; because there is a possibility of crash
The button should fire the IBAction method that you have set in IB. Have you checked by inserting an NSLog or by setting a breakpoint if the method is entered?
Is the button set to be initially activated in IB?
The crux with the IB construction is that the IBOutlet gets its value when the cell is loaded from the xib. The IBOutlet will then be the button in the cell that the tableview happend to load last.
WHen you add the button programatically, you might need to have the previous added button to removeFromSuperview before you add a button again to that cell.
In my case, the problem was that the button was a subview of the background view.
Buttons assigned to the background view apparently can't respond to touches.

UIViewController Problem with IB

This is what I do
Setup a view with UIViewController Subclass
Go to IB, and pull a view into the iphone
Pull a navigation bar over the UIView
Pull a tableview on the space below.
Everything appears well when I am running the interface builder.
But these things do not work.
If I add a button on top of the Navigation Bar, the button appears but IBAction is not called. The button seems to be at a level beneath the Navigation Bar (somewhere else)
If I add a footer view for the tableView, it does not appear. I believe the UIView is again masking it or something...
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *viewForFooter = [[UIView alloc] init ];
UIButton *footerButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 40, 100, 40)];
footerButton.titleLabel.text = #"Forgot Login";
//self.forgotLogin = footerButton;
[viewForFooter addSubview:footerButton];
return viewForFooter;
}
WHY ?
1) No reaction to click: Try to add the action in code. If it works, something was wrong with connecting up the IBAction.
[self.barButton setTarget:self];
[self.barButton setAction:#selector(IBActionHandler:)];
As a backup, you can also try to add the button in code:
UIBarButtonItem *barButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
selector:#selector(IBActionHandler:)];
self.navigationItem.rightBarButtonItem = barButton;
[barButton release];
2) Invisible button - I think your button is a custom UIButton, so it is transparent. The title is not visible because you have to set it like this:
[footerButton setTitle:#"Forgot Login" forState:UIControlStateNormal];
If you want a regular rounded angle button you have to set the frame after creating it.
UIButton *footerButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// Careful, this button is autoreleased.
[footerButton setFrame:CGRectMake(200, 40, 100, 40)];
BTW, something might also be wrong with your CGRects. The footerView should perhaps also be properly initialized with a frame, and you might have to implement
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
as well.
PS: To go with a standard UITableViewController might actually be a good idea.
Happy coding!

iPhone: Find Button at Custom Row

(Have a question...A have a table view xib and a custom row xib which I use in Table View...In Custom row xib I put 2 buttons (Button1 and Button2) by Interface Builder...So when I run my app a have a Table View with a lot of rows..and in every row I have 2 buttons...What I need is when I press Button1 in current row, to set Button2 Highlighted in the same row...
I just simply created an IBAction to do that:
- (IBAction)buttonTouchIn:(id)sender{
if (sender == self.button1) {
[button2 setHighlighted:YES];
}
But the problem is that I cannot get button2 in the same row with button1...All the time used button2 in last row...Any suggestions ? Thanks...
set tag property in Interface Builder for your buttons, let's say 11 for button1 and 12 for button2
get your button2 with the tag
- (IBAction)buttonTouchIn:(id)sender{
if ([sender tag] == 11){
UITableViewCell* cell = (UITableViewCell*)[[sender superview] superview];
UIButton* button2 = (UIButton*)[cell viewWithTag:12];
[button2 setHighlighted:YES];
}
}
I edited this code on my PC, so there could be errors
Use this method. Tag your button with a specific tag let's assume 11 for button 1 and 12 for button 2. And In an IBACtion write this code to get the cell
- (IBAction) btn1Tapped:(id)sender {
UIButton *btn = (UIButton*)sender;
UITableiewCell *cell = [[btn superview] superview];
//Now you can get the button with tag no 12
UIButton *btn2 = [cell viewWithTag:12];
[btn setHighlighted:YES];
}
Well with the lil info provided i can guess the problem as follows:
You are reusing your cells in uitable view
Probably assigning self.button2 in your cellforrowatindex delegate method
So the last button is getting highlighted. You should create a view controller for your tableview cell and write IBactions for the button press events in that view controller. This way every action on button goes to a seperate viewcontroller.
Let me know if this hint is useful or if you need more directions.

What is a good way for a custom accessory view in a UITableViewCell to message the table view controller?

I am working on adding a custom accessory view, (a button) to a UITableViewCell, and I need it to tell the table view when it is touched, but I can't figure out how to communicate to the table view what button was pressed. Ideally I'd like to somehow call a function like this:
[controller tableView:view didSelectCustomButtonAtIndexPath:indexPath usingCell:self];
when my custom view button is pressed.
Sorry if this is a bit vague, I'm not really sure how to explain this well. I am basically looking for how to mimic the implementation for tableView:didSelectRowAtIndexPath: without having to subclass UITableViewCell.
Thanks for any help.
When I had to do this, I did it like so:
//when adding accessoryView to cell in cellForRowAtIndexPath:
UIButton *b = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[b addTarget:self action:#selector(doAction:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = b;
//the handling method
- (void)doAction:(id)sender {
NSIndexPath *indexPath = [table indexPathForCell:(UITableViewCell *)[sender superview]];
// code goes here, indexPath tells you what row was touched
}
Should work with a control other than a UIButton.