Hi in my Application I'm displaying the data form my local database sqlite in UITableView . In that data's I have integer value displaying in my UILabel. Now to want to increment and decrement the value by clicking button I have tried but its not working properly its only working one time when click the next row again its showing the old value please tell me how to resolve this one. I have created two UIButtons dynamically for increment and decrement.
My database data fetch code.
qtt=[[NSMutableArray alloc]init];
[self openDB];
NSString *sql =[NSString stringWithFormat:#"SELECT * FROM br"];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK) {
while (sqlite3_step(statement)== SQLITE_ROW) {
char *field5 = (char *)sqlite3_column_text(statement, 4);
NSString *field5Str =[[NSString alloc]initWithUTF8String:field5];
str2 = [[NSString alloc]initWithFormat:#"%#",field5Str];
[qtt addObject:str2];
}
}
My UITableView display code.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier =#"Cell";
displayCell *cell =(displayCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[displayCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.ttt.text= [self.qtt objectAtIndex:indexPath.row];
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=indexPath.row;
[cell.contentView addSubview:button];
button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.frame = CGRectMake(200,25, 40.0, 40.0);
[button1 setTitle:#"-" forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(aMethod1:)
forControlEvents:UIControlEventTouchUpInside];
button1.tag=indexPath.row;
[cell.contentView addSubview:button1];
if([valueString isEqualToString:[self.qtt objectAtIndex:indexPath.row]])
{
int value=[valueString integerValue];
if (self.check==YES)
{
value=value+1;
str2=[NSString stringWithFormat:#"%d",value];
NSLog(#"%#",str2);
}
else
{
if (value==1)
{
}
else
{
value=value-1;
str2=[NSString stringWithFormat:#"%d",value];
NSLog(#"%#",str2);
}
}
cell.ttt.text=str2;
}
return cell;
}
My UIButton Action method code.
-(void)aMethod:(id)sender
{
self.check=YES;
int tag=[sender tag];
valueString=[self.qtt objectAtIndex:tag];
[self.mytableview reloadData];
}
-(void)aMethod1:(id)sender
{
self.check=NO;
int tag=[sender tag];
valueString=[self.qtt objectAtIndex:tag];
[self.mytableview reloadData]:
}
The problem is that you keep creating buttons every time that you go through the tableView:cellForRowAtIndexPath: code. When you call reloadData the first time, enough cells are available for reuse, so your code adds new buttons on top of the old ones; this happens every time you click [+] or [-].
In order to resolve this issue, make creation of the buttons conditional. Give your buttons different tags - say, 100 for [+] and 101 for [-]. This would let you find a subview by tag, and avoid creating new buttons when they are already there. Since you need that tag in your aMethod: and aMethod1:, put the tag of indexPath.row on the cell itself, and use int tag = [sender.superview tag] expression to retrieve it.
Here is a skeleton of how you can implement this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier =#"Cell";
displayCell *cell =(displayCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[displayCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.ttt.text= [self.qtt objectAtIndex:indexPath.row];
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];
}
if ([cell.contentView viewWithTag:101] == nil) {
button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.frame = CGRectMake(200,25, 40.0, 40.0);
[button1 setTitle:#"-" forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(aMethod1:)
forControlEvents:UIControlEventTouchUpInside];
button1.tag=101;
[cell.contentView addSubview:button1];
}
cell.contentView.tag = indexPath.row;
// Remove the increment/decrement code, it does not belong here
}
Your event handler methods would look like this:
-(void)aMethod:(id)sender {
int tag=[[sender superview] tag];
int value=[[self.qtt objectAtIndex:tag] integerValue]+1;
[self.qtt replaceObjectAtIndex:tag withObject:[#(value) stringValue]];
[self.mytableview reloadData];
}
-(void)aMethod1:(id)sender {
int tag=[[sender superview] tag];
int value=[[self.qtt objectAtIndex:tag] integerValue]-1;
if (value <= 0) value = 1;
[self.qtt replaceObjectAtIndex:tag withObject:[#(value) stringValue]];
[self.mytableview reloadData];
}
Related
I have created UITableview Programmatically and Populated array values dynamically. Here My Problem is, I created a UIButton Programmatically for zeroth row, It created well but When I am Scrolling the tableview button is Populating some other cells also. I don't Know how to solve that issue in my tableview. I want a UIButton on Only Zeroth index of the cell. I have tried the below code..
- (IBAction)MoreBttn:(id)sender {
NSUserDefaults *userDefaults1 = [NSUserDefaults standardUserDefaults];
[userDefaults1 setObject:amenties forKey:#"FACILITIES"];
CGRect screenRect = [[UIScreen mainScreen] bounds];
amentiestableview=[[UITableView alloc]initWithFrame:screenRect style:UITableViewStylePlain];
amentiestableview.delegate=self;
amentiestableview.dataSource=self;
UIEdgeInsets insets = UIEdgeInsetsMake(20,10,20,10);
CGRect tableframe=amentiestableview.frame;
amentiestableview.frame=UIEdgeInsetsInsetRect(tableframe,insets);
[[UIApplication sharedApplication].keyWindow addSubview:amentiestableview];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [amenties count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"facilities";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if(indexPath.row == 0) {
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
float X_Co = self.tableView.frame.size.width - 60;
[b setFrame:CGRectMake(X_Co, 0.0, 60, 45)];
[b setTitle:#"button" forState:UIControlStateNormal];
[b addTarget:self
action:#selector(hidetableview:)
forControlEvents:UIControlEventTouchUpInside];
[b setImage:[UIImage imageNamed:#"clearpink.png"] forState:UIControlStateNormal];
[cell addSubview:b];
}
else{
}
cell.textLabel.text=[amenties objectAtIndex:indexPath.row];
return cell;
}
-(void)hidetableview:(UIButton*)sender
{
amentiestableview.hidden=YES;
}
The cell is being re-used, so the button appear on other cells. Try to update cellForRow method as follows:
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
float X_Co = self.tableView.frame.size.width - 60;
[b setFrame:CGRectMake(X_Co, 0.0, 60, 45)];
[b setTitle:#"button" forState:UIControlStateNormal];
[b addTarget:self
action:#selector(hidetableview:)
forControlEvents:UIControlEventTouchUpInside];
[b setImage:[UIImage imageNamed:#"clearpink.png"] forState:UIControlStateNormal];
[cell addSubview:b];
}
if(indexPath.row == 0) {
b.hidden = false
}
else{
b.hidden = true
}
We are recycling the cells by using the method dequeueReusableCellWithIdentifier. And you should recycle cells because the allocation of views need a lot of resources
Please try the below code
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if(indexPath.row == 0) {
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
float X_Co = self.tableView.frame.size.width - 60;
[b setFrame:CGRectMake(X_Co, 0.0, 60, 45)];
[b setTitle:#"button" forState:UIControlStateNormal];
[b addTarget:self
action:#selector(hidetableview:)
forControlEvents:UIControlEventTouchUpInside];
[b setImage:[UIImage imageNamed:#"clearpink.png"] forState:UIControlStateNormal];
b.tag = 1010
[cell addSubview:b];
}
else{
cell.contentView.viewWithTag(1010).removeFromSuperview()
}
Here is my problem, i have a custom cell in that 1 label and 4 buttons like question and answers,when the user click on the button 1 remaining 3 button go like this,
When the user scroll the cell it is not saving the what the user selected button i mean to say user click on the what ever the button it is not saving.
Here is my code,
ViewDidLoad()
{
testArray = [[NSMutableArray alloc]init];
for (int i =0; i<[mainArray count]; i++)
{
[testArray addObject:#"Unchecked"];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = #"Cell";
ContestQATableViewCell *cell =(ContestQATableViewCell *)[tableViewQA dequeueReusableCellWithIdentifier:cellId];
if (cell==nil)
{
NSArray *myNib;
myNib =[[NSBundle mainBundle]loadNibNamed:#"ContestQATableViewCell" owner:self options:nil];
cell = (ContestQATableViewCell *)[myNib lastObject];
}
cell.textLabel.text = [mainArray objectAtIndex:indexPath.row];
if([[testArray objectAtIndex:indexPath.row] isEqualToString:#"Unchecked"])
[cell.answer1 setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
else
[cell.answer1 setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
cell.answer1.tag = indexPath.row;
[cell.answer1 addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:cell.answer1];
return cell;
[cell.answer1 addTarget:self action:#selector(buttonsClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.answer2 addTarget:self action:#selector(buttonsClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.answer3 addTarget:self action:#selector(buttonsClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.answer4 addTarget:self action:#selector(buttonsClicked:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
and here is my add target method
-(void)buttonsClicked:(id)sender
{
UIButton *btn=(UIButton *)sender;
ContestQATableViewCell * cell=(ContestQATableViewCell *) [btn.superview superview];
if (cell.answer1.tag==btn.tag)
{
[cell.answer1 setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
}else{
[cell.answer1 setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
}
if (cell.answer2.tag==btn.tag)
{
[cell.answer2 setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
}else{
[cell.answer2 setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
}
if (cell.answer3.tag==btn.tag)
{
[cell.answer3 setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
}else{
[cell.answer3 setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
}
if (cell.answer4.tag==btn.tag)
{
[cell.answer4 setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
}else{
[cell.answer4 setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
}
}
For Check-Uncheck functionality only buttonClicked: method is not enough. You will have also put the condition in cellForRowAtIndexPath: method for which button is selected or which in unselected because cellForRowAtIndexPath: method will call each time when you will scroll your UITableView and cells will be refresh.
for example
I explain in step by step
Step-1
Create the two arrays one for gloabally another one for checking purpose
#interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
{
NSMutableArray *arrayforCheckUnchek; // handle which button is selected or which is unselected
NSMutableArray *originalArray; // original Array
}
Step-2
allocating the memory for arrays
- (void)viewDidLoad
{
[super viewDidLoad];
arrayforCheckUnchek = [[NSMutableArray alloc]init];
originalArray = [[NSMutableArray alloc]initWithObjects:#"cell1",#"cell2",#"cell3",#"cell4",#"cell5", nil];
// setting all cell initilayy at un check
for(int i=0; i<[originalArray count]; i++)
{
[arrayforCheckUnchek addObject:#"Unchecked"];
}
}
Step-2
setup your datasource Methods
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [originalArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *Identifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [originalArray objectAtIndex:indexPath.row];
UIButton *radiobutton = [UIButton buttonWithType:UIButtonTypeCustom];
[radiobutton setFrame:CGRectMake(270.0, 7.0, 30.0, 30.0)]; // customize the frames
if([[arrayforCheckUnchek objectAtIndex:indexPath.row] isEqualToString:#"Unchecked"])
[radiobutton setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
else
[radiobutton setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
radiobutton.tag = indexPath.row;
[radiobutton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:radiobutton];
return cell;
}
Step-3
here selected button index you can change the image
-(void)buttonClicked:(UIButton *)sender
{
//Getting the indexPath of cell of clicked button
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:yourtableviewname];
NSIndexPath *indexPath = [yourtableviewname indexPathForRowAtPoint:touchPoint];
//Checking the condition button is checked or unchecked.
if([[arrayforCheckUnchek objectAtIndex:indexPath.row] isEqualToString:#"Unchecked"])
{
[sender setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
[arrayforCheckUnchek replaceObjectAtIndex:indexPath.row withObject:#"Checked"];
}
else
{
[sender setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
[arrayforCheckUnchek replaceObjectAtIndex:indexPath.row withObject:#"Unchecked"];
}
}
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
First off tags wont work. I say this because i create 4 buttons all with the same tag for a specific cell i.e indexPath.row = tag.
Inside My TableViewCellForRowAtIndexpath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"courseCell";
//Step 1: Check to se if we can reuse a cell from a row that has just rolled off the screen
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//step 2: If there are no cell to reuse, create a new one
if (cell == nil){
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
...
//-------------Creation of Custom Buttons-------------//
//-----img = "radioOn.png"-----//
//----img2 = "radioOff.png"----//
//----RadioButtonA----//
...
radioButtonA = [UIButton buttonWithType:UIButtonTypeCustom];
[radioButtonA addTarget:self action:#selector(radioButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
radioButtonA.tag=indexPath.row;
//----End RadioButtonA----//
//----RadioButtonB----//
radioButtonB = [UIButton buttonWithType:UIButtonTypeCustom];
[radioButtonB addTarget:self action:#selector(radioButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
radioButtonB.tag =indexPath.row;
...
//----End RadioButtonB----//
//----RadioButtonC----//
radioButtonC = [UIButton buttonWithType:UIButtonTypeCustom];
[radioButtonC addTarget:self action:#selector(radioButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
radioButtonC.tag = indexPath.row;
...
//----End RadioButtonC----//
//----RadioButtonNA----//
radioButtonNA = [UIButton buttonWithType:UIButtonTypeCustom];
radioButtonNA.tag = indexPath.row;
[radioButtonNA addTarget:self action:#selector(radioButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
...
//----End RadioButtonC----//
//---------End of Radio Button Creations---------//
//---------UIStepper & StepperLabel Creation-----//
[cell.contentView addSubview:radioButtonA];
[cell.contentView addSubview:radioButtonB];
[cell.contentView addSubview:radioButtonC];
[cell.contentView addSubview:radioButtonNA];
//Step4: Return the cell
return cell;
}
#pragma mark - Buttons
- (void)radioButtonClicked:(UIButton *)sender
{
UIButton *myButton = sender;
// This Method and all the ones similar to this method is created to handle the UITouchUpInsideEvent that the user sends when pressing the radioButtons A-NA.
[radioButtons addObject:sender];
// Create an instance(object) of class NSIndexPath called indexPath and set its value the indexPath of the cell the user is currently in.
UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *indexPath = [myTableView indexPathForCell:cell];
// Initialize two unique variables in order to check if the buttons being clicked are being referenced in the correct index.
int row = indexPath.row;
NSLog(#"Button is in row %d", row);
...
}
-(IBAction)button:(UIButton*)sender{
...
#try {
for (i=0; i<8; i++) {
if ([credits count ] ==0) {
break;
}
HERE is where i am trying to access the radiobuttons i created in my cell. What i would like to do is this
if([credits objectAtIndex:i]) == radioButtonA{
do stuff. The reason im not saying == [radioButtonA tag] is because i have three other buttons all with the same tag. If your read the code u see why the tags are set this way.
}
What I am asking for is 1 help, and 2 is there another way to check if two Buttons i.e objects are equal without having to rely on their tags.
Do not worry about the Try catch finally i was using it to catch the exception being thrown.
if ([[[[credits objectAtIndex:i]titleLabel]text] isEqualToString:#"A"]) {
NSLog(#"radioA is in array");
creditHours+=[[valueArray objectAtIndex:i]doubleValue];
gradeEarned+=(GradeA.doubleValue *[[valueArray objectAtIndex:i]doubleValue]);
NSLog(#"%f",gradeEarned);
continue;
}
if ([[[[credits objectAtIndex:i]titleLabel]text] isEqualToString:#"B"]) {
NSLog(#"radioB is in array");
creditHours+=[[valueArray objectAtIndex:i]doubleValue];
gradeEarned+=(GradeB.doubleValue *[[valueArray objectAtIndex:i]doubleValue]);
continue;
}
if ([[[[credits objectAtIndex:i]titleLabel]text] isEqualToString:#"C"]){
NSLog(#"radioC is in array");
creditHours+=[[valueArray objectAtIndex:i]doubleValue];
gradeEarned+=(GradeC.doubleValue *[[valueArray objectAtIndex:i]doubleValue]);
continue;
}
if([credits objectAtIndex:i]== radioButtonNA){
NSLog(#"boboboobbobob");
continue;
}
}
}
#catch (NSException *exception) {
NSLog(#"NSException Caught");
NSLog(#"Name: %#",exception.name);
NSLog(#"Reason: %#", exception.reason);
}
#finally {
NSLog(#"in finally block");
}
// if ([credits objectAtIndex: i] == defaulter) {
// UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Failed to select all grades" message:[NSString stringWithFormat:#"Your grade selections have been reset"] delegate:self cancelButtonTitle:#"great"otherButtonTitles:nil];
// [alert show];
// [alert release];
// [self refreshArray];
// }
NSLog(#"%f",gradeEarned);
if (gradeEarned == 0) {
textLabel.text= [NSString stringWithFormat:#"%f",gradeEarned];
}else {
NSLog( #"boob");
sum= (gradeEarned)/(creditHours);
NSLog(#"%f",sum);
textLabel.text= [NSString stringWithFormat:#"%f",sum];
//[self refreshArray];
}
}
For more information Here is the log...
NSLog(#"%#",[credits objectAtIndex:i]);
NSLog(#"%#",radioButtonA);
THE First output is the log of the [credits object atIndex:i]
UIButton: 0x6c91430; frame = (86 110; 32 30); opaque = NO; layer = CALayer: 0x6c914f0
2012-06-20 20:24:01.568 TableView[12557:f803] UIButton: 0x6ea8ad0; frame = (86 110; 32 30); opaque = NO; tag = 6; layer = CALayer: 0x6e746e0
As you can see The UIBUttons are DIFFERENT thus == operator does not work
When checking two object against one another you cannot use ==, you have to use [objectA isEqual:objectB], if those two objects are the same the answer will be YES and NO if they are not.
To read more go to: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html
and check what is written for isEqual:
I'm not sure, in the method,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
you should add the buttons only when the cell is nil.just like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"courseCell";
//Step 1: Check to se if we can reuse a cell from a row that has just rolled off the screen
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//step 2: If there are no cell to reuse, create a new one
if (cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
//-------------Creation of Custom Buttons-------------//
//-----img = "radioOn.png"-----//
//----img2 = "radioOff.png"----//
//----RadioButtonA----//
...
radioButtonA = [UIButton buttonWithType:UIButtonTypeCustom];
[radioButtonA addTarget:self action:#selector(radioButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
radioButtonA.tag=indexPath.row;
//----End RadioButtonA----//
//----RadioButtonB----//
radioButtonB = [UIButton buttonWithType:UIButtonTypeCustom];
[radioButtonB addTarget:self action:#selector(radioButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
radioButtonB.tag =indexPath.row;
...
//----End RadioButtonB----//
//----RadioButtonC----//
radioButtonC = [UIButton buttonWithType:UIButtonTypeCustom];
[radioButtonC addTarget:self action:#selector(radioButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
radioButtonC.tag = indexPath.row;
...
//----End RadioButtonC----//
//----RadioButtonNA----//
radioButtonNA = [UIButton buttonWithType:UIButtonTypeCustom];
radioButtonNA.tag = indexPath.row;
[radioButtonNA addTarget:self action:#selector(radioButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
...
//----End RadioButtonC----//
//---------End of Radio Button Creations---------//
//---------UIStepper & StepperLabel Creation-----//
[cell.contentView addSubview:radioButtonA];
[cell.contentView addSubview:radioButtonB];
[cell.contentView addSubview:radioButtonC];
[cell.contentView addSubview:radioButtonNA];
}
...
//Step4: Return the cell
return cell;
}
I had already known this, however, the purpose of this post was to see if their are any other ways. I also used the isMemberOfClass:[UIButton class] to further narrow down. Best way and most effective way of doing this is using the object's tags to compare against.
Hello guys i have cells in tableview, but there are two different UIButtons to be assigned to cell. Depend on if the filename those cell indication is there in the sandbox.
i am doing this by following function while creating cell.
NSLog(#"%#", filepath);
if ([[NSFileManager defaultManager] fileExistsAtPath:filepath])
{
[button setTitle:#"Submit" forState:UIControlStateNormal];
[button addTarget:self action:#selector(SubmitData:) forControlEvents:UIControlEventTouchUpInside];
}
else
{
[button setTitle:#"Fetch" forState:UIControlStateNormal];
[button addTarget:self action:#selector(FetchData:) forControlEvents:UIControlEventTouchUpInside];
}
but the thing is that when i press Fetch button and when FetchData function is called, i fetch the data and save it in the sandbox. So i want to update the button to Submit as now the file is their.
So do i have to put something in FetchData function to update the cell button to Submit,
I tried using [self.tableview reloaddata];
but won't update the cell.
FULL CODE
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *FirstLevelCell= #"FirstLevelCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstLevelCell];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:FirstLevelCell] autorelease];
NSDictionary * assignment = [assignments objectAtIndex:[indexPath row]];
cell.textLabel.text= [NSString stringWithFormat:#"Riding Number is %#", [assignment objectForKey:#"Riding_Number"]];
cell.detailTextLabel.text= [NSString stringWithFormat:#"Poll Number is %#",[assignment objectForKey:#"Poll"]];
UIImage *buttonUpImage = [UIImage imageNamed:#"button_up.png"];
UIImage *buttonDownImage = [UIImage imageNamed:#"button_down.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, (buttonUpImage.size.width)*1.20,
buttonUpImage.size.height);
[button setBackgroundImage:buttonUpImage forState:UIControlStateNormal];
[button setBackgroundImage:buttonDownImage forState:UIControlStateHighlighted];
button.tag = [indexPath row];
NSString * filepath = [self dataFilePathwithFilename:[NSString stringWithFormat:#"%#_%#.plist",[assignment objectForKey:#"Riding_Number"],[assignment objectForKey:#"Poll"]]];
if ([[NSFileManager defaultManager] fileExistsAtPath:filepath])
{
[button setTitle:#"Submit" forState:UIControlStateNormal];
[button addTarget:self action:#selector(SubmitData:) forControlEvents:UIControlEventTouchUpInside];
}
else
{
[button setTitle:#"Fetch" forState:UIControlStateNormal];
[button addTarget:self action:#selector(FetchData:) forControlEvents:UIControlEventTouchUpInside];
}
cell.accessoryView = button;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
// Configure the cell...
return cell;
}
set the closing } for if (cell == nil) right after cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:FirstLevelCell] autorelease];
it says in your comment where you should configure the cell!.
the if-not-nil statement is for creation only!! after this block you have to configure the cell (set texts, buttons, images, accessoryView, ...). Just remember the cell could be in ANY state. When a cell leaves the visible area (topmost cell when you scroll down), it will be put in a pool where you grab it out again with dequeueReusableCellWithIdentifier and configure it to match the required index path (bottom cell that slides into the screen when you scroll down).