Hiding button on tableview on a particular cell - iphone

I have created table. On each cell I am adding button depending upon the value from flag array.
Here is my code for adding button on cell
if ([[self.flags objectAtIndex:indexPath.row]
isEqualToString:#"false"])
{
UIButton *btnFlag = [UIButton buttonWithType:UIButtonTypeCustom];
[btnFlag addTarget:self
action:#selector(flagButtonPressed:)
forControlEvents:UIControlEventTouchDown];
btnFlag.backgroundColor = [UIColor redColor];
[btnFlag setTitle:#"flag" forState:UIControlStateNormal];
btnFlag.frame = CGRectMake(230, 40, 50, 20);
[cell addSubview:btnFlag];
}else if ([[self.flags objectAtIndex:indexPath.row] isEqualToString:#"true"])
{
UIButton *btnFlag = [UIButton buttonWithType:UIButtonTypeCustom];
[btnFlag setHidden:YES];
[cell addSubview:btnFlag];
}
-(void)flagButtonPressed:(id)sender {
UITableViewCell *owningCell = (UITableViewCell*)[sender superview];
indexPathToCell = [self.cotDetailsTbl indexPathForCell:owningCell];
// some stuff }
In the above method Im calling the api and depending upon the response obtained Im replacing the value in flag array from false to true. Once the value in flag array becomes true Im reloading table.
But the problem is when Im reloading table the button still appears its not hidden
Thanks

In your else part you again creating a new instance of button using UIButton *btnFlag = [UIButton buttonWithType:UIButtonTypeCustom] instead you need the existing UIbutton instance and set it to hidden.
Try using the following:
In if statement, assign a tag to each button
btnFlag.tag = indexPath.row
Now. in else do the following find the button by its tag and set hidden :
else if ([[self.flags objectAtIndex:indexPath.row] isEqualToString:#"true"])
{
UIButton *btnFlag = [cell.contentView viewWithTag:indexPath.row];
[btnFlag setHidden:YES];
}

Related

How to save a reference to a control added programmatically?

I added 3 buttons programmatically to my view, then I added the buttons to an array so that I can access them at a later time:
for (i = 0; i < 3; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[view addSubview:button];
[_buttons addObject:button];
}
If I reference the button in my array and change the image of the button, it does not change the button on screen.
UIButton* button = [_buttons objectAtIndex: 0];
[button setImage:thumb forState:UIControlStateNormal];
I've found a way to change the image of the button by looping through all the subviews in my view, but is there a better way?
for (UIView* subView in ((UIView*)[self.view.subviews objectAtIndex:0]).subviews){
if ([subView isKindOfClass:[UIButton class]]){
UIButton *button = (UIButton*)subView;
if (button.tag == self.selected){
[button setImage:thumb forState:UIControlStateNormal];
}
}
}
A common reason for this is that your array has not been initialized. When this happens, Objective C does not complain or throw excetions: instead, it behaves as if the calls to add elements never happened. It also returns nil when you try getting items back.
Add this line to your viewDidLoad method:
_buttons = [NSMutableArray array];
This should solve the problem.
Possible this could help on a click event:
-(void)clickEvent:(id)sender
{
[sender setImage:thumb forState:UIControlStateNormal];
}
If this is how you are picking up the event.
When you do addSubview, view retains subview you are adding. Also addObject retains it. Hence both are different ojbects. Changing properties of object in array will not affect object retained by view.
You can avoid loop by using tags. While adding buttons on view set unique tags to them. And when you want to access them, get it using tag directly.
//set tags for buttons
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTag:999];
[view addSubview:button];
//access using tag
UIButton *button = (UIButton*)[view viewWithTag:999];
[button setImage:thumb forState:UIControlStateNormal];
Add a tag to the button after creating it. And later use that tag to get that button.
for (i = 0; i < 3; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag = i+1;
[view addSubview:button];
[_buttons addObject:button];
}
Then you can access it like:
UIButton *button1 = (UIButton *)[self.view viewWithTag:1];
UIButton *button2 = (UIButton *)[self.view viewWithTag:2];
UIButton *button3 = (UIButton *)[self.view viewWithTag:3];
While adding UIControl programatically to use reference later add tag to it which should be different like:
yourBtn.tag = 111;
Now get reference of UIButton like this:
UIButton *button = (UIButton*)[yourViewWhereYouAdded viewWithTag:111];

Add a Button to a TableView Cell:

I have a UI tableView and I am trying to create a button like the one used in the Safari Settings to Clear History:
The code I have tried doesnt really work and I'm sure there is a better way:
case(2):
// cell = [[[ButtonLikeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if (indexPath.row == 0)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:cell.bounds];
[button addTarget:self action:#selector(clearButtonClick) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundColor:[UIColor purpleColor]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitle:#"Clear Data" forState:UIControlStateNormal];
cell.accessoryView = nil;
cell.clipsToBounds=YES;
[cell.contentView addSubview:button];
}
break;
}
}
return(cell);
}
A subclass on the tableview cell is overkill for this, basically all you need to do is set the text alignment of the textLabel to center
case(2):
if (indexPath.row == 0)
{
cell.textLabel.textAlignment = UITextAlignmentCenter;
}
break;
no need to use an actual UIButton.
the only difference between does rows and "normal" rows is the position of the label.
subclass UITableViewCell and override -laysubviews to center the label and you're done, like so:
#interface ButtonLikeCell : UITableViewCell
#end
#implementation ButtonLikeCell
-(void)layoutSubviews
{
self.titleLabel.center = self.center;
}
#end
this will reposition the label of the cell and make it look similar to what you're looking for.

how to create custom button on table view cell

I want to create custom button in table view.in that by clicking on that button the label will be create with current date.In that button i also have to implement checkbox functionality. I have implemented following code for checkbox functionality:It works fine but i can i create label by checking them YES.Thanks in advance.
UIButton *btnUncheck=[[UIButton alloc] initWithFrame:CGRectMake(260, 35, 20, 20)];
//btnUncheck=[UIButton buttonWithType:UIButtonTypeCustom];
btnUncheck.tag=indexPath.row;
[btnUncheck setImage:[UIImage imageNamed:#"NO.png"] forState:UIControlStateNormal];
[btnUncheck addTarget:self action:#selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];
[btnUncheck release];
[cell.contentView addSubview:view];
return cell;
}
-(IBAction)checkBoxClicked:(id)sender
{
if(favoriteChecked==NO)
{
[sender setImage:[UIImage imageNamed:#"YES.png"] forState:UIControlStateNormal];
favoriteChecked=YES;
[lblAchievementreward setHidden:NO];
}
else
{
[sender setImage:[UIImage imageNamed:#"NO.png"] forState:UIControlStateNormal];
favoriteChecked=NO;
}
Yes, you can. In checkBoxClicked:, save the row of the checked item in an instance variable. Then, reload the row so that cellForRowAtIndexPath is called again. Something like-
self.checkedRow = sender.tag;
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathWithIndex:sender.tag]] withRowAnimation:UITableViewRowAnimationNone];
Then, in cellForRowAtIndexPath:, check for self.checkedRow and add a label. Something like-
if(indexPath.row == self.checkedRow)
{
UILabel* label = [[UILabel alloc] init];
//label.text = [NSDate date]; //use date formatters here
[cell.contentView addSubview:label];
}

Adding button into UITableViewCell

I am trying to add button into my table cell like below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
....
UIButton* buttonCheckbox = [UIButton buttonWithType:UIButtonTypeCustom];
buttonCheckbox.frame = CGRectMake(0, 177, 56, 26);
buttonCheckbox.backgroundColor = [UIColor redColor];
[buttonCheckbox addTarget:self action:#selector(actionFirst1) forControlEvents:UIControlEventTouchUpInside];
[buttonCheckbox setTitle:#"MyTitle" forState:UIControlStateNormal];
[cell addSubview:buttonCheckbox];
but actionFirst1 event is not fired. if i add the button [self.view addSubview:buttonCheckbox] instead of [cell addSubview:buttonCheckbox] it works fine. why?
thanks...
You should add this button to the contentView property of your cell
[cell.contentView addSubview:buttonCheckbox];
In fact, all your custom elements inside a UITableViewCell should be placed inside the contentView as this cell elements (contentView, accessorView and image) are all placed on top of any elements that your cell have.
Check this doc for more info on cell about custom UITableviewCell
Hi try below code it may help you out:
if ([indexPath row] == 0)
{
UIButton *myButton = [[UISwitch alloc]initWithFrame:CGRectMake(x, y, width,
heigth)];
[cell addSubview:myButton];
cell.accessoryView = myButton;
[myButton addTarget:self action:#selector (actionFirst1)
forControlEvents:UIControlEventTouchUpInside];
}
This code will help you out. Please set frames according to your cell of TableView.
Please see whether it would suffice to add your button as the accessoryView of the table cell. In that case, the iOS framework will position the button in the right place for your cell style itself (though you still need to give it a size). Here's an example from one of my projects:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(0, 0, 100, 27); // only size matters
[myButton setTitle:#"Connect" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(connect:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = myButton;
That's it. You just create the control, make sure it has a size, and set it to the accessoryView.

change button state in cellForRowAtIndexPath

this is driving me MAD now.. I have a UItableview. Based on an NSMutableArray, I populate it.
I set up in reuseTableViewCellWithIdentifier with the following
cellRectangle = CGRectMake((ARROW_OFFSET + 5), (ROW_HEIGHT - LABEL_HEIGHT) / 2.0, ARROW_WIDTH, LABEL_HEIGHT);
UIButton *tmpButton = [[UIButton alloc] initWithFrame:cellRectangle];
[tmpButton initWithFrame:cellRectangle];
[tmpButton setImage:[UIImage imageNamed:#"icon_edit.png"] forState:UIControlStateNormal];
[tmpButton setImage:[UIImage imageNamed:#"icon_no.png"] forState:UIControlStateDisabled];
[tmpButton addTarget:self action:#selector(editSelectedRow:) forControlEvents:UIControlEventTouchUpInside];
tmpButton.tag = ARROW_TAG;
[cell.contentView addSubview: tmpButton];
[tmpButton release];
then in cellForRowAtIndexPath I have the following lines of code
UIButton *button = (UIButton *)[cell viewWithTag:ARROW_TAG];
[button setTag:indexPath.row];
if (counterHasStarted == 1) {
NSLog(#"yes");
button.enabled = NO;
} else {
button.enabled = YES;
}
the button shows nicely, but for some reason when the counterHasStarted variable (which is an int is set, it doesn't change! I can change UILabels based on the above code (checking if counterHasChanged is 1 or 0).
Any ideas what's going on??
-cellForRowAtIndexPath: will only get called when the table view needs a new UITableViewCell because the user scrolled.
I guess you're changing counterHasStarted and expect the button enabled state to change? You could reload the data when you change counterHasStarted ([yourTableView reloadData]). Then the table view will call -cellForRowAtIndexPath: for all the cells that are currently visible, and you can enable or disable the buttons as needed.