Adding buttons to a UITableView cell - ios

I am trying to add buttons to my UITableView cells, using code like below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
UIButton *button = self.buttons[0];
[cell addSubview:button];
return cell;
}
The button isn't showing though. Is it possible to add buttons to a cell like this or do I have to make a custom UITableViewCell class?
When the button was made for the array the frame was set like below:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 70)]

The usage for adding elements have changed somewhen. Use
[cell.contentView addSubview:button];
and don't forget to proof before if a button is already added :P
enum
{
kButtonForA = 3333 // example
};
for every button set
button.tag = kButtonForA;
and use them
UIButton* btn = (UIButton *)[cell.contentView viewWithTag:kButtonForA];
if(!button)
btn = self.buttons[i];
UPDATE
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if(!self.buttons || [self.buttons count] == 0)
return 0;
return ...;
}
and after you init your array, use
[tableView reloadData];
just one possible way, but a fast one

try this in your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([cell.contentView viewWithTag:100] == nil) {
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(130,25, 40.0, 40.0);
[button setTitle:#"+" forState:UIControlStateNormal];
[button addTarget:self action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
button.tag=100;
[cell.contentView addSubview:button];
}
then perform action
-(void)aMethod:(id)sender {
NSInteger tag=[[sender superview] tag];
//NSInteger value=[[self.qtt objectAtIndex:tag] integerValue]+1;
//[self.qtt replaceObjectAtIndex:tag withObject:[#(value) stringValue]];
//[self.mytableview reloadData];
}

Related

How to detect multiple buttons in tableview cell

How to detect multiple buttons in tableview cell and my doubt is with example i have 3 buttons in cell if i tap on one button that button will change colour and and if i click indexpath.row=1 cell button that button will color also need to change help me
I did like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(10.0, 0.0, self.tableView.frame.size.width/4, 40.0);
button.tag = 100 + indexPath.row*total_buttons_in_a_row;
[button setTitle:[NSString stringWithFormat:#"%ld",(long)button.tag] forState:UIControlStateNormal];
[button addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button2.frame = CGRectMake(10.0+self.tableView.frame.size.width/4+10.0, 0.0, self.tableView.frame.size.width/4, 40.0);
button2.tag = 100 + indexPath.row*total_buttons_in_a_row + 1;
[button2 setTitle:[NSString stringWithFormat:#"%ld",(long)button2.tag] forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button2];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button3.frame = CGRectMake(10.0+self.tableView.frame.size.width/4*2+10.0, 0.0, self.tableView.frame.size.width/4, 40.0);
button3.tag = 100 + indexPath.row*total_buttons_in_a_row + 2;
[button3 setTitle:[NSString stringWithFormat:#"%ld",(long)button3.tag] forState:UIControlStateNormal];
[button3 addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button3];
return cell;
}
-(void)btnClicked:(UIButton *)sender{
id selectedButton = [self.view viewWithTag:sender.tag];
if ([selectedButton backgroundColor] == [UIColor redColor]) {
[selectedButton setBackgroundColor:[UIColor clearColor]];
}else{
[selectedButton setBackgroundColor:[UIColor redColor]];
}
}
total_buttons_in_a_row is an Int. In your case define it in viewDidLoad total_buttons_in_a_row=3
P.S - set the Buttons CGRectMake according to your need.
Assign tag to each button using the combination of section & row..
When method assigned to button get called use '%' & '/' for further manipulation.
Let me know if you have any difficulty in implementing it..
Got to your CustomCell.h & right below #import add this code.
#protocol CustomCellDelegate <NSObject>
- (void)buttonActionwith :(NSIndexPath *)indexPath;
#end
Then Add this code right below #interface CustomCell : UITableViewCell
//Manual Properties
#property (strong, nonatomic) NSIndexPath *buttonIndexPath;
//Delegate
#property (nonatomic, weak) id <CustomCellDelegate> delegate;
The use of this NSIndexPath is to identify which cell user selecting.
Then go to CustomCell.m & in your button IBAction method add this code.
[self.delegate buttonActionwith:self.buttonIndexPath];
The meaning of this line of code is that, when the user TouchUpInside the Button in the cell buttonActionwith delegate method calls, if you set this CustomCellDelegate in your UIViewController where the UITableView is, that delegate method will call inside that ViewController too.
Now go to your MainViewController.h & add
CustomCellDelegate
& it will look like this after that,
#interface MainViewController : UIViewController <CustomCellDelegate,,UITableViewDataSource,UITableViewDelegate>
when creating the Custom UITableViewCell in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath don't forget to add this line of code before return cell;
//Set Cell Values Here
cell.delegate = self; //Setting delegate to self
cell.buttonIndexPath = indexPath;
The modified - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath will look like this,
static NSString *MyIdentifier = #"MyIdentifier"; //Set Identifier for cell
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier: MyIdentifier]; //init CustomCell
if (cell == nil) { //Check cell is nill or not
NSArray *nib;
nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell"
owner:self options:nil]; //if cell is nil add CustomCell Xib
for (id oneObject in nib) if ([oneObject isKindOfClass:[CustomCell class]])
cell = (CustomCell *)oneObject;
}
//Set Cell Values Here
cell.buttonIndexPath = indexPath
return cell;
Finally add this method inside MainViewController.m
#pragma mark - SwipeableCellDelegate
- (void)buttonActionwith:(NSIndexPath *)indexPath { //Delegate method
NSLog(#"Button Clicks at index %ld",(long)indexPath.row);
}
This is how to add a single button inside the Cell. you can use this method to more buttons to the cell.

How to select radio button at a time in table view

I am having a radio button in table view cell. This is my radio button
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"myCell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"myCell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIButton *newRadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
newRadioButton.frame = CGRectMake(30, 0, 15, 14.5);
[newRadioButton setImage:[UIImage imageNamed:#"unselect"] forState:UIControlStateNormal];
[newRadioButton setImage:[UIImage imageNamed:#"select"] forState:UIControlStateSelected];
cell.accessoryView = newRadioButton;
if ([indexPath isEqual:selectedIndex])
{
newRadioButton.selected = YES;
}
else
{
newRadioButton.selected = NO;
}
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedIndex = indexPath;
[table reloadData];
}
-(void)radiobtn:(id)sender
{
if([sender isSelected])
{
[sender setSelected:NO];
} else
{
[sender setSelected:YES];
}
}
Its working,But i want to select only one radio button at a time like this image (left). But for me selecting all the radio button (right).
I need to make it look like the first image.
I think you should try:
in YourViewController.h
#interface YourViewController : UITableViewController {
NSIndexPath *selectedIndex
}
in YourViewController.m
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"myCell"];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"myCell"];
UIButton *newRadioButton;
newRadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
newRadioButton.frame = CGRectMake(30, 0, 15, 14.5);
[newRadioButton setImage:[UIImage imageNamed:#"unselect"] forState:UIControlStateNormal];
[newRadioButton setImage:[UIImage imageNamed:#"select"] forState:UIControlStateSelected];
cell.accessoryView = newRadioButton;
}
if ([indexPath isEqual:selectedIndex]) {
newRadioButton.seleted = YES;
} else {
newRadioButton.seleted = NO;
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
and
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedIndex = indexPath;
[tableView reloadData];
}
this link may help you
Unselect all the radio buttons before your if statement using something like this:
for (UITableViewCell *cell in [self visibleCells]) {
[[cell accessoryView] setSelected:NO];
}
When you are creating the button in cellForRowAtindexpth, attach an unique feature by which you can identify which button is being tapped. To do this UIButton has tag property
you need to set the tag for everybutton inside cellForRowAtindexpth, something like
// where i is just a static int variable for holding the count
myRadioButton.tag = i ;
i=i+1
now you when you tap based on tag, you can check which button is pressed and you can select that one
to know which button is pressed u can know like this
-(IBAction)buttonPressed:(id)sender{
UIButton *button = (UIButton *)sender;
NSLog(#"%d", [button tag]);
}
Try to reuse button
Solution: we can add & access controls by tag from view
if control found by tag we can use it.
if control not found we have to add it with tag.
UIButton *newRadioButton = (UIButton *)cell.accessoryView;
if (!newRadioButton || ![newRadioButton isKindOfClass:[UIButton class]]) {
UIButton *newRadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
newRadioButton.frame = CGRectMake(30, 0, 15, 14.5);
[newRadioButton setImage:[UIImage imageNamed:#"unselect"] forState:UIControlStateNormal];
[newRadioButton setImage:[UIImage imageNamed:#"select"] forState:UIControlStateSelected];
cell.accessoryView = newRadioButton;
}

How to change image for UIButton after user click on Done button is another UIView in iOS

When the user clicks on a UIButton on tableview ( tableview has mutiple button in each row), another View is shown.
I want to change image for this button after the user has clicked on the Done button of the other UIView. How can I do that? I'm a newbie. Could you please provide some code for me? Thanks in advance.
UPDATE CODE:
Code for tableview :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"%d,%d",indexPath.section,indexPath.row];
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
[market addTarget:self action:#selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
[market setTag:3000];
[market setFrame:CGRectMake(200, 6, 30, 30)];
[cell.contentView addSubview:market];
}
for (UIButton *button in [cell subviews]) { // change name of table here
if ([button isKindOfClass:[UIButton class]]) {
button.tag = indexPath.row;
[button setImage:[UIImage imageNamed:#"Marketplace.png"] forState:UIControlStateNormal];
}
}
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
_tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
return cell;
}
Code for Open button ( button that user click on to show another View)
- (void)marketPressedAction:(id)sender
{
UIButton *button = (UIButton *)sender;
buttontag = button.tag;
NSLog(#"Market button click at row %d",buttontag);
}
And code for Done button:
-(void)submitMarket
{
for (UIButton *button in [_tableView subviews]) { // change name of table here
if ([button isKindOfClass:[UIButton class]]) {
if (button.tag == buttontag) {
[button setBackgroundImage:[UIImage imageNamed:#"MarketplaceSelect.png"] forState:UIControlStateNormal];
}
}
}
}
Try this :
[yourButton setBackgroundImage:someImge forState:UIControlStateNormal];
Code on button click :
savedTag = button.tag;
Code on Done button click :
for (UIButton *button in [table subviews]) { // change name of table here
if ([button isKindOfClass:[UIButton class]]) {
if (button.tag == savedtag) {
[button setBackgroundImage:someImge forState:UIControlStateNormal];
}
}
}
In cellForRowAtIndexPath in place of this : [market setTag:3000]; write [market setTag:indexPath.row];
Try this :
Replace :
marketButton = (UIButton *)[cell.contentView viewWithTag:3000];
[marketButton setTag:indexPath.row];
With this :
for (UIButton *button in [cell subviews]) { // change name of table here
if ([button isKindOfClass:[UIButton class]]) {
button.tag == indexPath.row;
}
}
One more thing : make first line of cellForRowAtIndexPath as :
NSString *CellIdentifier = #"tableCell";
Change your cellForRowAtIndexPath as :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
[market addTarget:self action:#selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
[market setImage:[UIImage imageNamed:#"Marketplace.png"] forState:UIControlStateNormal];
[market setTag:indexPath.row];
[market setFrame:CGRectMake(200, 6, 30, 30)];
[cell.contentView addSubview:market];
}
else {
for (UIButton *button in [cell subviews]) { // change name of table here
if ([button isKindOfClass:[UIButton class]]) {
button.tag = indexPath.row;
}
}
}
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
_tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
return cell;
}
you can use a delegate or a NSNotificationCenter,
in your view where is Done button, you need create a property
#property (weak, nonatomic) <protocol_name> delegate;
and in your action method for DoneButton, you need to send a message to that delegate
[self.delegate method_name];
and the other view will be set as delegate for this one
`[viewWithDoneButton setDelegate:self];
and you need to implement the delegate method `
You can set another image for selected state of your button and after click on Done button just set selected state your button.
[yourButton setBackgroundImage:someImge forState:UIControlStateSelected];
after Done button:
[yourButton setSelected:YES];
Or if you use before UIControlStateSelected you can also use any other state. Or even users state - UIControlStateApplication

Add permanent checkmark to tableView-cell

my viewController contains a tableView where the user can add cells via coreData.
I defined the cells:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Travel";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSManagedObject *travel = [self.travelling objectAtIndex:indexPath.row];
UILabel *countryLabel = (UILabel *)[cell viewWithTag:101];
kategorieLabel.text = [travel valueForKey:#"country"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(15.0f, 32.0f, 24.0f, 20.0f);
[cell addSubview:button];
if ([[travel valueForKey:#"tick"] isEqualToString:#"yes"]) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}else{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
return cell;
}
- (void)buttonTapped:(UIButton *)sender {
UITableViewCell *owningCell = (UITableViewCell*)[sender superview];
[owningCell setAccessoryType:UITableViewCellAccessoryCheckmark];
NSIndexPath *indexPath = [_tableView indexPathForCell:owningCell];
NSManagedObject *travel = [self.travelling objectAtIndex:indexPath.row];
[travel setValue:#"yes" forKey:#"tick"];
}
When the user taps a button on the cell, the checkmark should appear. But when I reload or restart the app not all checkmarks are there. Maybe there is a better method to do this.

How to tap on Image view from a cell which is in a table view

I have a image view(tapping on which i get a popover) inside a cell associated in table views.
Log element tree captures the static text 'Picture' but it doesn't show associated image view with it.
My flow is like tables -> cells( have around 4 cells which are static texts)
These cells in turn have textfields attached with them.
cell1-> textfields1
cell2-> textfields2
cell3-> textfields3
cell4-> has just an image.
How do I access this Image?
var imageId = table[0].cells()[4];
imageId.images();
Dis not return anything.
i think u can configure ur cell like this:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
CGRect Frame4 = CGRectMake(10.0, 30.0, 50.0, 40.0);
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
button.frame = Frame4;
[button setTitle:#"Click" forState:UIControlStateNormal];
button.backgroundColor = [UIColor grayColor];
[button addTarget:self
action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
return cell;
}
and then add button action event :
(IBAction)buttonPressed:(UIButton *)sender{
// in sender u can get which button is pressed
//do something
}
please replace " UIButtonTypeDetailDisclosure " for ur desire image Button

Resources