Dynamically change UITableViewCell's accessory view image - objective-c

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 !!

Related

ChildView didn't maintain its ParentView Frame

Hope all of you will be fine. Guys i have a situation, I have a uiview and uibutton as under:
UIView* containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 70)];
containerView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:containerView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:#selector(SettingPressed:) forControlEvents:UIControlEventTouchUpInside];
UIImage *image = [UIImage imageNamed:#"settings_button.png"];
[button setImage:image forState:UIControlStateHighlighted & UIControlStateNormal & UIControlStateSelected];
button.frame = CGRectMake(0,0, 40, 40);
button.center = containerView.center;
[containerView addSubview:button];
The problem is uibutton appear far from uiview instead of in the middle of uiview. What i am mistaking, please guide me.
Thanks.
First, when you change a view's center property the frame property will be changed automatically so you only should set the button center:
button.center = CGPointMake(containerView.frame.size.width / 2,
containerView.frame.size.height / 2);
Now, whenever you change the center property you need to manually tell the view to redraw itself using setNeedsDisplay :
[button setNeedsDisplay]
and lastly, the difference between frame and bounds that frame defines the position and size relative to the parent view while bounds describes the drawing area inside (or outside) the frame.

how do add a uibutton and position it at bottom of the UITableView?

newbie question,
I have created a uitable programmatically, and need to add a submit button at bottom of the table, setting mybutton.frame = CGRectMake(xxxx) does not related to my table, any idea? This is my code for the uitable
CGRect tableViewFrame = self.view.bounds;
self.myTable = [[UITableView alloc] initWithFrame:tableViewFrame
style:UITableViewStyleGrouped];
self.myTable.autoresizingMask = UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.myTable];
The simplest way is to just set it as the footerView for your table. Create the button normally, then add it as the footer view to your table
UIButton * b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[b setTitle:#"Submit" forState:UIControlStateNormal];
[b addTarget:self action:#selector(submitButtonClicked) forControlEvents:UIControlEventTouchUpInside];
self.myTable.tableFooterView = b;
You then just need to implement the submitButtonClicked function to do whatever you want when the button is clicked.
To place your button at the bottom of your tableView, try this:
UIButton *submit = [UIButton buttonWithType:UIButtonTypeCustom];
[submit setFrame:CGRectMake(0, tableViewFrame.origin.y + tableViewFrame.size.height, 80, 80);
[[self view] addSubview:submit];

Display UIButton mutually in UITableView

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];

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 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.