Display UIButton mutually in UITableView - objective-c

I'd like to display UIButton only in UITableView at even indexPath.row.
But, UIButton appears in all cells.
if (indexPath.row % 2 == 0) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(160, 140, 80, 40);
[button setTitle:#"button" forState:UIControlStateNormal];
[cell addSubview:button];
}
How can I resolve this problem?
Thanks.

First make sure what is the height of the cell :
You may be facing the issue because of height of button.
It should be more or equal to the Button's height.
Additionally replace your code with this.
if (indexPath.row % 2 == 0) {
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(160, 4, 80, 40)];
[button setTitle:#"BtnName" forState:UIControlStateNormal];
[cell.contentView addSubview:button];
}

Your code is correct but you are doing one very small mistake in it. You are trying to add the button on the cell instead of doing that, try to add the button on the contentView of the cell as follows.
[cell.contentView addSubview:button];

Related

Dynamically change UITableViewCell's accessory view image

I have a CustomTableView Cell and it has an accessory view. How can I change the accessory view's image dynamically?
Use your custom button for accessoryview like..
// stick the pencil icon on the accessory view
UIButton* pencil = [UIButton buttonWithType:UIButtonTypeCustom];
[pencil setImage:[UIImage imageNamed:#"icon-pencil.gif"] forState:UIControlStateNormal];
pencil.frame = CGRectMake(0, 0, 15, 15);
pencil.userInteractionEnabled = YES;
[pencil addTarget:self action:#selector(didTapEditButton:) forControlEvents:UIControlEventTouchDown];
cell.accessoryView = pencil;
do not use accessoryView, just place another imageView and change accordingly
In the init method of the custom cell, you can do something like (assuming ARC btw):
UIImage *customBtnImg = [UIImage imageNamed:#"custom_btn"];
UIButton *customBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, customBtnImg.size.width, customBtnImg.size.height)];
customBtn.backgroundColor = [UIColor clearColor];
[customBtn setBackgroundImage:customBtnImg forState:UIControlStateNormal];
self.accessoryView = customBtn;
modifications to the cell except textColor and detailTextColor of cell should be done in the willDisplayCell: method and not the cellForRowAtIndexPath method ,
the solution for your question is as follows --
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cellforIndexPath:(NSIndexPath *)indexPath
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageName:#"image.png" forControlState:UIControlStateNormal];
btn.frame = CGRectMake(0,0,28,28) ;
cell.accessoryView = btn ;
}
this code will definitely work !!

how do I left align a button in UITableViewCell

How do I align the table cell button to the left instead of the right? Below is the code I am using to create the UIButton. Thanks.
cell.accessoryView = [self getButton: #"icon.png"];
...
- (UIButton*)getButton: (NSString*)icon {
UIImage *image = (true) ? [UIImage imageNamed:icon] : [UIImage imageNamed:icon];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:self action:#selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
return button;
}
You are adding the button as an accessory view, which cannot be put on the left. See this question, it is almost exactly the same:
iOS UITableViewCell accessory on the left side?
Then you can muck with the frame of the button.
UIButton *cellButton = [self getButton:#"icon.png"];
[cell.contentView addSubview:cellButton];

how to make button round with image background in IPhone?

I have a round button, but i will make it have a backgroud with image.
but when i do it, by setting the (what) property to an image, the button becomes rectangle because the image is a rectangle. How do I keep the button round?
Simply by doing this
#import <QuartzCore/QuartzCore.h>
myButton.layer.cornerRadius = 8;
Tested Code:
.h
-(void)vijayWithApple;
.m
-(void)vijayWithApple{
NSLog(#"vijayWithApple Called");
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Micky.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(vijayWithApple) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(135.0, 180.0, 40.0, 40.0);//width and height should be same value
button.clipsToBounds = YES;
button.layer.cornerRadius = 20;//half of the width
button.layer.borderColor=[UIColor redColor].CGColor;
button.layer.borderWidth=2.0f;
[self.view addSubview:button];
Restut
Best solution I have found especially for UITableCell. I put the code in awakeFromNib and worked right off the bat.

How to create a UITableViewCell that contains a single UIButton taking up most of the cell's space?

I'm trying to create a UITableViewCell that contains a single big button.
I tried what seemed obvious, adding a UIButton to the cell's contentView, but it didn't work (the cell is displayed empty). What am I dong wrong?
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"startButtonCell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"startButtonCell"] autorelease];
UIButton *btn = [[UIButton alloc] initWithFrame:cell.contentView.bounds];
[cell.contentView addSubview:btn];
if (!self.task.isCompleted) {
btn.titleLabel.text = #"Start!";
}else{
btn.titleLabel.text = #"Continue!";
}
[btn release];
}
Try the following:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:cell.contentView.frame];
[btn setTitle:#"Button Title" forState:UIControlStateNormal];
[cell.contentView addSubview:btn];
I am by far no expert but I think the problem with your solution is that by default the button style of UIButton is UIButtonTypeCustom, which is invisible because it is not customized yet. If I change the code above from
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
to
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
I get get the same result you do. No Button visible. If you would like to use UIButtonTypeCustom you would have do do some customization. Adding a background image to your button for example.

Help with iphone button pressed

I am trying to see which button was clicked on so I can preform the correct logic.
This is the code for the buttons:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(270, 423, 60, 60)];
[button addTarget:self action:#selector(buttonPressedAction:)
forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[[UIImage imageNamed:#"refreshicon.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0]
forState:UIControlStateNormal];
button.tag = 1;
UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(0, 423, 60, 60)];
[button2 addTarget:self action:#selector(buttonPressedAction:)
forControlEvents:UIControlEventTouchUpInside];
[button2 setBackgroundImage:[[UIImage imageNamed:#"login.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0]
forState:UIControlStateNormal];
button2.tag = 2;
[self.navigationController.view addSubview:button];
[self.navigationController.view addSubview:button2];
And this is how I call the buttonPressedAction:
- (void)buttonPressedAction:(id)sender
{
UIButton* button = (UIButton*)sender;
if(button.tag == 1)
{
NSLog(#"1");
}else
{
NSLog(#"2");
}
}
But when I use NSLog to see what the sender value is, it crashes.
Any advice for what's happening and how to correct it?
Now corrected :o) THANKS!
As other have pointed out, UIButton does not have a value property. I guess you are trying to detect which button was clicked. Here are two ways to do this:
Use the tag property one each button. I.e. button1.tag = 1, button2.tag = 2. You can then test which button was clicked with if(sender.tag == 1) etc. You could introduce constants for the numbers to make the code more readable.
If you keep a reference to the button, you can check if the reference is equal. Example: if(sender == self.button1)
Probably the best approach would be to give buttons that do different things different actions, but failing that, you could distinguish them by tag.
- (void)buttonPressedAction:(id)sender
{
UIButton* button = (UIButton*)sender;
// do something
}
If you were saving the pointers to the button objects you are creating, you could compare the pointers. Or you can just set the tag property for the button, and use that to switch behaviour.