How to determine if a cell contains a button - iphone

I've created a cell named CELL .
My first task is add a button on the cell, which I have successfully added.
However, while retrieving the CELL I need to know if the cell is the reusable cell or not. If not, then create the cell and add the button, but if the cell exists and button does not, I need to add the button.
In my XIB I added the button in the cell. In some cells I need to show the button and in some I don't.
How can I determine if the cell contains a button? And if it does contain a button can I tag it?

Use -
for( int i =0 ; i < [cell.subviews count]; i++) {
if ([[cell.subviews objectAtIndex:i] isKindOfClass:[UIButton Class]] ) {
//Button is found, do whatever you want
UIButton *button = [cell.subviews objectAtIndex:i];
int tag = button.tag;
}
}

One approach is to iterate though subview which I personally think is bad idea.
The other approach is to set the tag of the cell say 100 for containing button.
Then you can check
if (cell.tag == 100) {
// Cell with button
} else {
// Cell without button
}

Related

How to increment a label value when click a button

In my iPad app.i want to increment a label value when click a button.
actually label values are sending from previous view. so now i clicked a button but the last label value was incremented. see the below picture.
in the above picture i want to increment the label value in between the minus(-) and plus(+) buttons. but when i clicked plus button in first view but the label value is incremented on the third view.
** the above three views shown on the picture are sub viewed the scroll view **
i'm using this code......
-(IBAction)plusbutton:(id)sender
{
val = [sender tag];
NSLog(#"the_tag %d",val);
itemref.countVal++;
[self createOrderView];
}
According to you
fist give same tag value for - and + button and label in that single view so when click on the button
in button action
NSArray *subviews=[self.scrollview subviews];
for(UIView *sb_local in subviews)
{
if(sb_local.tag==[sender tag])
{
if([sb_local isKindOfClass:[UILabel class]])
{
UILabel *new_label=(UILabel *)[sb_local viewWithTag:pictag];
new_label.text = #"your value";
//[new_label.text intValue]you get int value from that label.Increment or decrement that int value according your (+,-)button actions and assign again it to that label.
}
}
}
Please place this code in ibaction + and - buttons please give different actions for + and - buttons in each view

Every TableView Cell has an image, some are `hidden`, how can i test if all them are not hidden?

In my app, i have a table view and every row/cell has an imageView already on it. Each imageView already has its image assigned to it, each imageView's .hidden property is set to YES until otherwise specified by the user. My question is: How can i test to see if any images are hidden/not hidden. If 1 cell has a visible image, i want to Call a method. And if all the images are gone i want to fire a different method, Any ideas?
I was trying something along the lines of:
if ([self.hand.showBadge boolValue] == YES) {
self.handView.hidden = YES;
}
else{
self.handView.hidden = NO;
}
But unfortunately it didnt work.
Thank you!
One way could be to tag the imageView w/in the cell and then use that tag to iterate over the cells as needed.
Within tableView:cellForRowAtIndexPath, add:
myImageView.tag = MY_CELL_IMAGEVIEW_TAG;
Alternatively, you could just set the tag for your image view in a prototype cell (or your static cells), if using storyboards.
Then, if you wanted to check the visible cells, insert the following code where you'd like to check the image views.
BOOL cellImageViewsAreHidden = YES;
for (UITableViewCell *cell in self.tableView.visibleCells) {
UIImageView *cellImageView = [cell viewWithTag:MY_CELL_IMAGEVIEW_TAG];
if ([cellImageView isHidden] == NO) {
cellImageViewsAreHidden = NO;
break;
}
}
(cellImageViewsAreHidden) ? 'method for all hidden' : 'method for not all hidden' ;

Getting the data of a particular row with a button infront of it and passing that data to the clickButton function

I am currently in the cellForRow method where I have a button infront of each row.
CellForRow:
JSONDecoder *decoder=[[JSONDecoder alloc] initWithParseOptions:JKParseOptionNone];
NSMutableArray *json=[decoder objectWithData:myData]; {This has a list of items in the dictionary format}
NSDictionary *dict=[json objectAtIndex:indexPath.row];
NSString *status=[dict valueForKey:#"status key in json"];
"dict" can be represented as:
{
status key in json="approved";
name="abc";
type="resourceName";
},
{
status key in json="rejected";
name="xyz";
type="bus";
},
{
status key in json ="pending";
name="pqr";
type="resource";
}...and so on .
Printing status will give me the status of all the rows.
approved
rejected
pending
But I only need the status of that particular row infront of which I am gonna click the button. This is because I need that status for each row separately to send it to the buttonPressed method when I click the button infront of that row. I don't want to do that in didSelectRow method.
How can I get the indexPath.row of a particular row(on clicking that row) so that I could write the method on click of the button corresponding to that particular row?
You just have to add the button as subview for your cell, and pass indexpath.row as a button tag.Thats it.
I have given the sample
//Create ur cell then do the following
UIButton *objBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[objBtn setFrame:CGRectMake(100,5,50,50)];
[objBtn addTarget:self action:#selector(methodWhichGetsCallOnButtonClick:)forControlEvents:UIControlEventTouchUpInside];
objBtn.tag = indexPath.row;
[cell addSubview:objBtn];
Now in "methodWhichGetsCallOnButtonClick" method add your business logic.
Also take a look on this question.
EDIT:
Also See this link
Try this out, and revert me back if need any more help.
If I am not wrong you want indexPath of cell on button clicked.You can get the cell of that button(in button action) as follows:
UIButton *theButton = sender;
CreatedCell *cell = (CreatedCell *)[[theButton superview] superview];
After getting cell, you can get indexPath of that cell as follows:
theIndexPath = [[m_TableView indexPathForCell:cell] retain];
You can use tag to UIButton.
In cellForRowAtIndexPath Method
You set tag to Button like this
btn.tag=indexpath.row;
In Button Action method you can retrive tag value using this line
Action method for Button
-(void)btnAction:(id)sender
{
UIButton *btn=(UIButton *) sender;
int tagValue=btn.tag;
NSDictionary *dict=[json objectAtIndex:tagValue];
// From this dictionory you cann acces the stauts key value
}
I hope it works for you

How to distinguish dynamically generated button on selection using iPhone SDK?

In my iPhone app, I have array of buttons that are dynamically generated based on user selection.
How do I distinguish selected button from others?
I want that when user select the other button the previously selected button should go back to its normal state in terms of its looks. I am unable to revert the previously selected buttons to its normal state.
Use tag to identify the button.
At the time of creating buttons you can assign tag as number to the button and use the same to identify.
yourButton.tag = intNumber;
You have an array of the buttons. You can loop through your array and check if it is the one that was clicked.
- (IBAction) buttonClicked:(id)sender {
for(int i; i < [array count]; i++){
if((UIButton *)sender == (UIButton *)[array objectAtIndex:i])
//do something
else
//do something else
}
Something like that.
Try setting tag for each button,using
yourButton.tag=intValue; //intValue>0
Your buttonAction should be as follows,
-(IBAction)buttonAction:(id)sender
Save the previously selected tag, and change the value accordingly.
you can loop through your subviews and set for all the old style:
- (void)highlightImgWithID:(int)packID {
[self.view.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[UIImageView class]]) {
[(UIImageView*)obj setHighlighted:([obj tag] == IDtoSelectNext)];
}
}];
}
The sample is how I currently implement it in my App with UIImageView's you can change it to work with buttons.

Wipe a delete button out of table view cell

when pressing a row delete button on a table view, I do some validation, and if the user chooses to cancel the operation it all should rollback. Not only want to keep that row (what is happening), but also make disappear the delete button leaving only the "-" round button. How can I do that?
once again, thank you.
Assuming you are implementing your validations in tableView:commitEditingStyle:forRowAtIndexPath: method of your UITableViewDatasource protocol object, you should be able to set the editingAccessoryType and editingAccessoryView on the cell.
//After validation fails....
UITableViewCell *aCell;
aCell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
// validations are done and you need to ignore the delete
if ( aCell.showingDeleteConfirmation ){
aCell.editingAccessoryView = nil;
aCell.editingAccessoryType = UITableViewCellAccessoryNone;
}
If you want, you can wrap the changes in an animation block to animate the change.
Alternatively, you could toggle the editing state of the cell.
//After validation fails....
UITableViewCell *aCell;
aCell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
if ( aCell.showingDeleteConfirmation ){
aCell.editing = NO;
aCell.editingAccessoryView = nil;
aCell.editing = YES;
}