UIButton to intercept UITableView's didSelectRowAtIndexPath method - iphone

I've got a UIButton on a table cell that is meant to pop up a UIActionSheet but the problem is the didSelectRowAtIndexPath captures that touch and its action takes precedence. Any clever way to override that action when the user touches the button and still have the default action when the user presses elsewhere in the cell?
Too bad there is no MoveToFront property.

How you have add button on cell
i am putting one sample
in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
...
UIButton *btnDetail = [UIButton buttonWithType:UIButtonTypeRoundedRect] ;
[btnDetail setFrame:CGRectMake(225.0f, 15.0f, 65.0f, 20.0f)] ;
//btnDetail.backgroundColor = [UIColor grayColor];
[btnDetail setTitle:#"Detail" forState:UIControlStateNormal];
[btnDetail setTitleColor: [UIColor redColor] forState: UIControlStateNormal];
[btnDetail.titleLabel setFont:[UIFont fontWithName:#"Verdana-Bold" size:12]];
[btnDetail setBackgroundColor:[UIColor clearColor]];
[btnDetail sizeThatFits:btnDetail.frame.size];
[cell addSubview:btnDetail];
[btnDetail addTarget:self
action:#selector(function1:)
forControlEvents:UIControlEventTouchUpInside];
[btnDetail setImage:[UIImage imageNamed:#"btn-detail.png"] forState:UIControlStateNormal];
if you have written
[btnDetail addTarget:self
action:#selector(function1:)
forControlEvents:UIControlEventTouchUpInside];
your button should able to capture touch event and function1 should be called... on it's touch event...

Related

Keep a UIButton selected for a fraction of time in iPhone?

How can I keep a custom UIButton highlighted on selection, for about 5 seconds and then redirect to other view? In my code it is not showing any color when selected rather redirecting to other view.
This is my code :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGRect rect=CGRectMake(0+70*j,yy,79,40);
UIButton *button=[[UIButton alloc] initWithFrame:rect];
[button setFrame:rect];
[button setContentMode:UIViewContentModeLeft];
button.titleLabel.font = [UIFont systemFontOfSize:14];
NSString *settitle=[NSString stringWithFormat:#"%#",item.TimeStart];
[button setTitle:settitle forState:UIControlStateNormal];
NSString *tagValue=[NSString stringWithFormat:#"%d%d",indexPath.section+1,i];
button.tag=[tagValue intValue];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setShowsTouchWhenHighlighted:YES];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside ];
[hlcell.contentView addSubview:button];
[button release];
}
-(IBAction)buttonPressed:(id)sender
{
[sender setBackgroundColor:[UIColor cyanColor]];
Login *lgn=[[Login alloc] init];
[self presentModalViewController:lgn animated:NO];
[lgn release];
}
How can I do it ?
You have to set setShowsTouchWhenHighlighted:yes to the UIButton.
for example:
UIButton *click_btn=[UIButton buttonWithType:UIButtonTypeCustom];
click_btn.frame=CGRectMake(238,10, 70, 70);
[click_btn setTag:indexPath.row];
[click_btn setShowsTouchWhenHighlighted:YES];
[click_btn addTarget:self action:#selector(sample) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:click_btn];
you just add inside that method sleep(5);
for example:
This is user sample uibutton action method.
-(void)sample{
sleep(5);
sampleclass *classobject=[[sampleclass alloc]init];
[self.navigationController pushViewController:classobject animated:YES];
}
I wouldn't suggest using sleep() as this sleeps all threads and makes the app essentially dead for x seconds.
Highlight the button as per the instructions above and then use
[self performSelector:#selector(sample) withObject:self afterDelay:5.0f];
-(void)sample
{
UIViewController *myNewController = [[UIViewController alloc]init];
enter code here
[self presentModalViewController:myNewController animated:YES];
}

Putting a button in table footer view

I am creating a custom button and putting it in my table's footer view but the button is going way out from the right corner.
What is wrong here!?!
Here is my code and output image:
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[aButton setTitleColor:[UIColor colorWithWhite:0.0 alpha:0.56] forState:UIControlStateDisabled];
[aButton setBackgroundImage:[[UIImage imageNamed:#"test.png"] stretchableImageWithLeftCapWidth:kButtonSliceWidth topCapHeight:0] forState:UIControlStateNormal];
[aButton setTitle:#"Click me" forState:UIControlStateNormal];
[aButton.titleLabel setFont:[UIFont boldSystemFontOfSize:kFontSize14]];
[aButton setFrame:CGRectMake(10.0, 15.0, 300.0, 44.0)];
[self.tableView setTableFooterView:aButton];
I added the button in the UIView and then set the tableFooterView to this UIView object and it worked. We cannot directly put UIButton as tableFooterView.
Try doing like this. Set IBOutlet UIView *footerView; and synthesize it. Add it as a subview using the method-
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
if(footerView == nil) {
//allocate the view if it doesn't exist yet
footerView = [[UIView alloc] init];
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(10, 3, 300, 44)];
//set title, font size and font color
[button setTitle:#"Click Me" forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
//set action of the button
[button addTarget:self action:#selector(clickPressed:)
forControlEvents:UIControlEventTouchUpInside];
//add the button to the view
[footerView addSubview:button];
}
//return the view for the footer
return footerView;
}
One important addition to Legolas' fine answer: You'll need to implement the tableView delegate method:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
foot 50;
}
This was driving me nuts because the button (or, in my case, buttons) were not responding to the touch event. Adding the delegate method made it all better. :-)

A custom Button in a UITableViewCell

I have a UITableView with three section. I have a custom UITableViewCell. I want to put a button in the first section of my table view to handle an action.
How to can do this please?
just create the button programmatically
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
You can also put the button directly into a UITableViewCell which is located in your first section. In your cellForRowAtIndexPath you can use for example:
UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[yourButton setTitle:#"Click me" forState:UIControlStateNormal];
[yourButton setFrame: CGRectMake( 20.0f, 3.0f, 280.0f, 33.0f)];
[yourButton addTarget:self action:#selector(clickAction) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:yourButton];
This example will look like this:
and will be places directly in the cell.
If you want to put it in the section header (rather than a cell), you could:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(section==0)
{
// create button and return it
}
}

Create button inside tableview cell without Interface Builder

I try to create a button inside the tableview cell without using Interface Builder.
I found this code that I thought it is for creating a button:
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(270,10,30,30);
[button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
}
but when I try to run the program nothing show up in the simulator.
am I missing something to create the button with this code?
You need to add the button to the cell - have you tried
[cell.contentView addSubview:button]
?
Adjust your button's frame to position it within the contentView and to size the button correctly.
You should write your all code in cellforRow At index Datasource method and for add your button on cell you can use this line of code
[cell.contentView addsubview:buttonobject];
button.frame = CGRectMake(10,10,30,30);
And also change Tour button background or style for your conformation it will be there or not.
It will surly work if its working inform us.
Your Welcome.
You have to add code in this method but first of all add custom cell to your table view,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
forwordBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[forwordBtn setTitle:#"Forward" forState:UIControlStateNormal];
[forwordBtn addTarget:self
action:#selector(forwordButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
forwordBtn.frame = CGRectMake(0, 0, 10, 10);
[cell.contentview addSubview:forwordBtn];
}

section header in uitableview - reusidentifier?

I have a table with customized headers for the sections. In addition to that, I implemented a collapsing feature, so that when a header is touched, all non-adressed sections are reduced, and the adressed section is expanded.
I realized that by using UIButtons as background views. Furthermore, the buttons have different colors and text, depending on their state (expanded vs not expanded).
I have a problem, similar to that reusidentifier problem, i.e. if u dont reuse cells already allocated in a table, certain fragments appear, if u start to scroll. Here it only happens to my first section, nevertheless it appears fragmented and duplicated....
Is there anything similar to reusing headerviews already allocated once like the method reusIdentifier for UITableViewCells?
Or is there a better approach?
Allocating the Buttons in advance to store them in array didnt do the trick for me...
Here's some code:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 40)];
[button setShowsTouchWhenHighlighted:NO];
[button setTitleShadowColor:[UIColor clearColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(collapse:) forControlEvents:UIControlEventTouchUpInside];
button.tag = section;
[button setAdjustsImageWhenHighlighted:NO];
button.backgroundColor = [UIColor colorWithRed:63.0f/255.0f green:154/255.0f blue:201/255.0f alpha:1.0f];
[button.titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:14.0]];
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[button setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
if (section == 0) {
// [button setBackgroundImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[button setTitle:#" Newsroom" forState:UIControlStateNormal];
} else {
[button setTitle:[NSString stringWithFormat:#" Sektion %d", section] forState:UIControlStateNormal];
}
if (section == mySection) {
[button setBackgroundImage:[UIImage imageNamed:#"sectionHeader_active.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"sectionHeader_active.png"] forState:UIControlEventTouchUpInside];
[button.titleLabel setTextColor:[UIColor whiteColor]];
} else {
[button setBackgroundImage:[UIImage imageNamed:#"sectionHeader_inactive.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"sectionHeader_inactive.png"] forState:UIControlEventTouchUpInside];
[button.titleLabel setTextColor:[UIColor colorWithRed:96.0f/255.0f green:96/255.0f blue:97/255.0f alpha:1.0f]];
}
return [button autorelease];
}
I also implemented a collapsing section table using UIButton header views. What I found was that it was folly trying to cache and resuse the views, because you don't know when the UITableView is done with them (I got crashes).
I recommend implementing your state change by: updating your data model; performing all the row insert/deletes to perform the collapses/expand; then doing a [tableView reloadData], which will call viewForHeaderInSection, where you use the state of your data model to return the appropriately formatted uibutton view.