Using custom UITableViewCell's accessory view to show two items - ipad

I've been looking for this for a while, but haven't found an answer (blame poor googling skills). I have a custom UITableViewCell class which, currently, consists of a custom UISwitch and a UILabel. I want to add a button that's only visible (And active) when the switch is set to "Yes". Right now I add the switch to the accessoryView, and leave it. However, the accessory view doesn't really have subviews, as far as I can tell, so here's my question:
Should I just create a UIView that has a button and a switch, size it to fit the cell's accessory view (or will it auto-size itself?), and put that in as the cell's accessory view? And is this typically the way that it's gone about?
Or is there a solution that I'm missing?
Thanks.

Here is an example:
UIButton* btdel = [[UIButton alloc] init];
btdel.tag = indexPath.row;
//[btdel setTitle:#"Delete Event" forState:UIControlStateNormal];
[btdel setBackgroundImage:[UIImage imageNamed:#"ButtonRemove.png"] forState:UIControlStateNormal];
[btdel addTarget:self action:#selector(deleteEvent:) forControlEvents:UIControlEventTouchUpInside];
// bt.titleLabel.frame = CGRectMake(0, 0, 95,24);
btdel.frame = CGRectMake(110, 0, 30,30);
[headerView addSubview:btdel];
[btdel release];
UIButton* bt = [[UIButton alloc] init];
bt.tag = indexPath.row;
[bt setTitle:#"Select a Dress" forState:UIControlStateNormal];
[bt setBackgroundImage:[UIImage imageNamed:#"findDress.png"] forState:UIControlStateNormal];
[bt addTarget:self action:#selector(showDresses:) forControlEvents:UIControlEventTouchUpInside];
bt.font=[UIFont systemFontOfSize:(CGFloat ) 13];
// bt.titleLabel.frame = CGRectMake(0, 0, 95,24);
bt.frame = CGRectMake(0, 3, 95,24);
[headerView addSubview:bt];
cell.accessoryView = headerView;

Related

I'm having custom tableview cell and I set a overall button at a bottom of a tableview

enter image description hereI'm having custom tableview cell and I want to set a overall button at a bottom of a tableview with clickable
UIButton*yourBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[yourBtn setImage:[UIImage imageNamed:#"wishlist"] forState:UIControlStateNormal];
//[yourBtn setTitle:#"+" forState:UIControlStateNormal];
yourBtn.frame = CGRectMake(0, self.view.bounds.size.height -100, buttonwidth, buttonheight);
yourBtn.center = CGPointMake(self.view.center.x, self.view.bounds.size.height -100);
[yourBtn addTarget:self action:#selector(addButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:yourBtn];
if i scroll tableview button also scroll from top to bottom.i want to set that button constant when scroll tableview
How to get a tableview button like this
You need to separate the tableview and the button.
Here is the code:
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
tableView.dataSource = self;
tableView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:tableView];
UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeCustom];
yourButton.frame = CGRectMake(CGRectGetWidth(self.view.frame) - 70,
CGRectGetHeight(self.view.frame) - 70,
50,
50);
[yourButton setImage:[UIImage imageNamed:#"Untitled-1"] forState:UIControlStateNormal];
[self.view addSubview:yourButton];
I hope the above information is helpful for you.

UIbutton not on the top of tableViewHeader isn't tapped

I want to make a tableViewHeader from xib.
In xib I have the following hierarchy of views
But this way ActionButton isn't tapped, nothing overlays it and userInteractionEnabled = YES for it.
The button becomes active only when I put it into ZeroScreenView, so the only workaround I found is to place button directly on the ZeroScreenView.
Does someone know why does this it happen?
Is there any way to make the button tappable when it lays onto Labels Container?
Don't know what problem you are getting. But through code I have done like this
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,40)];
headerView.backgroundColor = [UIColor grayColor];
UIButton *buttn = [UIButton buttonWithType:UIButtonTypeCustom];
[buttn setTitle:#"" forState:UIControlStateNormal];
[buttn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
buttn.titleLabel.font = CUSTOM_FONT_ROMAN(15);
[buttn setFrame:headerView.frame];
[buttn setBackgroundColor: [UIColor clearColor]];
[buttn addTarget:self action:#selector(tapButton) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:buttn];
self.tableView.tableHeaderView = headerView;

Get rid of padding for UITableViewCell custom accessoryView

I have a table view cell with a custom accessoryView, but it seems that the way it's laid out by default creates a 15-point margin on the right side. I want to get rid of it.
I can achieve the desired effect by overriding layoutSubviews, but that breaks edit mode animation (the accessory no longer slides in/out).
Any better way to do this?
Add subview instead of accessoryView
UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
deleteBtn.frame = CGRectMake(cell.contentView.frame.size.width-55, 2, 50, 50);
[deleteBtn setBackgroundImage:[UIImage imageNamed:#"trash.png"]
forState:UIControlStateNormal];
deleteBtn.backgroundColor = [UIColor clearColor];
//deleteBtn.alpha = 0.5;
deleteBtn.tag = indexPath.row;
[deleteBtn addTarget:self action:#selector(numberDeleteConfirm:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:deleteBtn];

adding multiple subviews in ios

I'm trying to add multiple subview programmatically, but my buttons inside those subviews are not working. The buttons were also added as subviews programmatically.
Here's the code, hopefully will explain more about what I mean.
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
[self setBackgroundColor:[UIColor clearColor]];
// UIView container of selected image with comment button.
UIView *containerOfSelectedImage = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 0, 350)];
NSLog(#"%f", self.frame.size.width);
// image view of selected photo by user.
UIImageView *userImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"nature_01.jpg"]];
[userImage setFrame :CGRectMake(0, 0, 340, 300)];
// comment button with action of recodering user's comment.
UIButton *commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
//[commentButton setBackgroundColor : [UIColor clearColor]];
float y = userImage.frame.size.height + 5.0f;
[commentButton setFrame : CGRectMake(32.0f, y, 24.0f, 24.0f)];
[commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchDown];
[commentButton setImage:[UIImage imageNamed:#"comments-24.png"] forState:UIControlStateNormal];
[containerOfSelectedImage addSubview:userImage];
[containerOfSelectedImage addSubview:commentButton];
[self addSubview:containerOfSelectedImage];
[self addSubView:self.commentContainerView];
return self;
You need to increase the frame of the containerOfSelectedImage. Currently the width is 0.
The containerOfSelectedImage's frame has to be big enough so the button fits inside it. If the button is outside that frame it will show up but you can't touch him.
To debug this issues you can use a tool like Reveal App. If any of your buttons is outside the frame of the parent then the touch will not be detected.
Change [commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchDown]; to [commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchUpInside];

Need to add multiple UI elements to tableheaderview of UITable in iOS

I have a UITableViewController that has a tableView. To this tableView, I would like to add both a label as well as a button but unfortunately, I am only able to add one or the other.
Here is my code that I am working with:
- (void) viewDidLoad {
[super viewDidLoad];
selectAllButton = [UIButton buttonWithType:UIButtonTypeCustom];
[selectAllButton setFrame:CGRectMake(280, 0, 25, 25)];
[selectAllButton setImage:[UIImage imageNamed:#"CheckBox1.png"] forState:UIControlStateSelected];
[selectAllButton setImage:[UIImage imageNamed:#"CheckBox2.png"] forState:UIControlStateNormal];
[selectAllButton setUserInteractionEnabled:YES];
[selectAllButton addTarget:self action:#selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
selectAllLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 40)];
[selectAllLabel setTextColor:[UIColor blackColor]];
[selectAllLabel setBackgroundColor:[UIColor clearColor]];
[selectAllLabel setText:#"Select All"];
self.tableView.tableHeaderView = selectAllButton;
self.tableView.tableHeaderView = selectAllLabel;
}
When I run the above method, I am only able to see my label which tells me it is replacing the button. Is there a way for me to get both elements to appear in the tableheaderview?
Create a UIView, add your button & label to it as subviews, than make that view your self.tableview.tableHeaderView
...your code
UIView *headerView = [UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, selectAllButton.frame.size.height + selectAllLabel.frame.size.height)];
[headerView addSubview:selectAllButton];
[headerView selectAllLabel];
self.tableView.tableHeaderView = headerView;
When you init your button & label be sure to change the frame as they will be based on the UIView
Since tableHeaderView is an UIView, try to add it with addSubview:(UIView*) or create an UIView and add that as your tableHeaderView.

Resources