I have Custom Table View Cell In That I Have Two UIButton and One Text Field Named As edit and cancel and one textField. When I Click on edit at same time TextFeild Interaction is Enabled And Cancel Button Image Should Change. Its Working Fine For Me!!
But When I am Clicking On Edit Button Another Cells Cancel bitton Image id Changed Automatically! I know that this happening because I'm reusing the cell!! But I'm not able to find a solution...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
simpleTableIdentifier = #"MenuNameCell";
cell = (MenuNameCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (!cell) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MenuNameCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
NSLog(#"---------new cell agin");
}
else
{
for (UIView *view in [cell.contentView subviews])
[view removeFromSuperview];
NSLog(#"---------older use");
// _checkButton = (UIButton *)[cell.contentView viewWithTag:indexPath.row];
// _cancelButton = (UIButton *)[cell.contentView viewWithTag:indexPath.row];
}
// Creating Label Menu Name
_nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 11, 82, 21)];
_nameLabel.backgroundColor = [UIColor clearColor];
_nameLabel.text = [_hotel._orderedMenus objectAtIndex:indexPath.row];
// Creating Label Menu Cost
_amountMenu = [[UILabel alloc] initWithFrame:CGRectMake(167, 13, 44, 21)];
_amountMenu.backgroundColor = [UIColor clearColor];
_amountMenu.text = [[_hotel._menuPrices objectAtIndex:indexPath.row] stringValue];
// Creating Text Field For Order Quantity
_textFieldQuantity = [[UITextField alloc] initWithFrame:CGRectMake(125,14,42,21)];
_textFieldQuantity.userInteractionEnabled = NO;
_textFieldQuantity.text = [[_hotel._selectedQuantity objectAtIndex:indexPath.row] stringValue];
// Creating Button For Check Order
_checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_checkButton setFrame:CGRectMake(232, 13, 25, 28)];
[_checkButton setBackgroundImage:[UIImage imageNamed:#"edit.png"]forState:UIControlStateNormal];
[_checkButton addTarget:self action:#selector(editQuantity:) forControlEvents:UIControlEventTouchUpInside];
[_checkButton setTag:indexPath.row];
// Creating Button For CANCEL Order
_cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_cancelButton setFrame:CGRectMake(265, 13, 25, 28)];
[_cancelButton setBackgroundImage:[UIImage imageNamed:#"cancel.png"] forState:UIControlStateNormal];
[_cancelButton addTarget:self action:#selector(cancelOreder:) forControlEvents:UIControlEventTouchUpInside];
[_cancelButton setTag:indexPath.row];
// Adding All To Call Content View
[cell.contentView addSubview:_nameLabel];
[cell.contentView addSubview:_amountMenu];
[cell.contentView addSubview:_textFieldQuantity];
[cell.contentView addSubview:_checkButton];
[cell.contentView addSubview:_cancelButton];
return cell;
}
Edit:
-(void)editQuantity:(id)sender
{
_textFieldQuantity.userInteractionEnabled = YES;
button = (UIButton *)sender;
row = button.tag;
NSLog(#"Check Button index is %d",row);
UIImage *buttonImage = [UIImage imageNamed:#"edit_over.png"];
[_checkButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
UIImage *buttonImageCancel = [UIImage imageNamed:#"check.png"];
[_cancelButton setBackgroundImage:buttonImageCancel forState:UIControlStateNormal];
_cancelButton.tag = 0;
}
-(void)cancelOreder:(id)sender{
button = (UIButton *)sender;
row = button.tag;
NSLog(#"The Row Selected iS At Cancel Order ISSSS----%d", row);
if (_cancelButton.tag == 0){
_textFieldQuantity.userInteractionEnabled = NO;
UIImage *buttonImageCancel = [UIImage imageNamed:#"check_over.png"];
[_cancelButton setBackgroundImage:buttonImageCancel forState:UIControlStateNormal];
UIImage *buttonImageCancel1 = [UIImage imageNamed:#"cancel.png"];
[_cancelButton setBackgroundImage:buttonImageCancel1 forState:UIControlStateNormal];
UIImage *buttonImage = [UIImage imageNamed:#"edit.png"];
[_checkButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
_cancelButton.tag = 1;
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"iHomeDelivery" message:#"Do You Want To Cancel the Order" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
Your problem is that _textFieldQuantity (and the others) is an instance variable. When editQuantity: and cancelOreder: are called the instance varialbe still has the same value that was set last time when adding a cell within cellForRowAtIndexPath: .
What could you do to fix it?
1st:
You could set the row in the tag of the button. Then, when the button is tapped on and the action is invoced get the tag and fetch the cell (you can call the table's cellForRowAtIndexPath method to get it. That is different from the delegate method that you implement) and then find the appropriate textField.
2nd:
A bit more work but probably smarter could be to implment a subclass of your table cell. Move the action methods to that very cell class. Doing so each button's action refers to an individual object wich is then the cell class and not the view controller. The cell class can have its own textField instance variables. It shoud even provide properties to the textField and other UIItems so that you can set their values in cellForRowAtIndexPath.
I would suggest the first solution as a quick starter. But for future use I strongly suggest to go the second way. That way will help you in similar situations in the future.
Related
I have a UITableView with two cells that have two buttons in them. The cells need to be independent of one another. Both Button1's will be selected by default. I need to be able to toggle the buttons in the same cell without affecting the other cell. For example, if I click on Button2 in Option2, Button1 will be deselected in the same cell while Option3's buttons remain unaffected. The code I have to create the buttons within the UITableView is below. The selector would toggle the buttons. And each cell would execute a different method based on which button is selected. How can I group the buttons based on the cell that they are located in?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *menuId = #"OptionsMenu";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:menuId];
if (cell == nil)
cell = AUTO_RELEASE([[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:menuId]);
cell.textLabel.text = [self.options objectAtIndex:indexPath.row];
if ([cell.textLabel.text isEqualToString:#"Option2"] || [cell.textLabel.text isEqualToString:#"Option3"]) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(setState:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Button1" forState:UIControlStateNormal];
button.tag = indexPath.row;
button.frame = CGRectMake(200.0f, 5.0f, 80.0f, 30.0f);
button.layer.borderWidth = 1.0f;
button.layer.cornerRadius = 4.0f;
button.layer.borderColor = [[UIColor grayColor] CGColor];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 addTarget:self action:#selector(setState:) forControlEvents:UIControlEventTouchDown];
[button2 setTitle:#"Button2" forState:UIControlStateNormal];
button2.tag = indexPath.row;
button2.frame = CGRectMake(300.0f, 5.0f, 80.0f, 30.0f);
button2.layer.borderWidth = 1.0f;
button2.layer.cornerRadius = 4.0f;
button2.layer.borderColor = [[UIColor grayColor] CGColor];
[cell addSubview:button];
[cell addSubview:button2];
}
return cell;
}
You want to make the cell the target of the control actions, not the table view. Then you can make the tableview a delegate of the cell. When the cell receives the setState: message, you have the cell tell the tableview via the delegate. You pass a pointer to the cell as part of the delegate message, and now the tableview knows what item to update in the model via indexPathForCell:.
I have add one SegementController in CellForRowAtIndex method and its selected lasted value which we select previously. But once we move to other screen and come back to current then Segement will select the multiple option like this :
Here "Yes" is already selected and then i select "No",
Here is the code which i write in CellForRowAtIndexPath :
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Yes", #"No", #"Either", nil]];
seg.frame = CGRectMake(190.0f, 10.0f, 205.0f, 30.0f);
seg.selectedSegmentIndex = 0;
[seg setTintColor:[UIColor orangeColor]];
seg.segmentedControlStyle = UISegmentedControlStyleBar;
[seg addTarget:self action:#selector(segSelected:) forControlEvents:UIControlEventValueChanged];
seg.tag = indexPath.row;
cell.textLabel.frame = CGRectMake(10, 5, 150, 30);
[cell.contentView addSubview:seg];
seg.selectedSegmentIndex = [[[arryaSegment objectAtIndex:indexPath.row]objectForKey:#"SelecteKey"] intValue];
This is probably not about selecting 2 segments at the same time, but adding two segmentedControls at the same time because you add a new control each time the cell is displayed.
Make sure the code that adds the UISegmentedControl is only called once per cell.
e.g:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell; // = ... dequeue ...
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
// add segmented control only when creating a new cell
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Yes", #"No", #"Either", nil]];
seg.frame = CGRectMake(190.0f, 10.0f, 205.0f, 30.0f);
seg.selectedSegmentIndex = 0;
[seg setTintColor:[UIColor orangeColor]];
seg.segmentedControlStyle = UISegmentedControlStyleBar;
[seg addTarget:self action:#selector(segSelected:) forControlEvents:UIControlEventValueChanged];
cell.textLabel.frame = CGRectMake(10, 5, 150, 30);
[cell.contentView addSubview:seg];
seg.tag = 42; // used to reference segmentedControl in dequeued cell. see next line
}
UISegmentedControl *seg = (UISegmentedControl *)[cell.contentView viewWithTag:42];
seg.selectedSegmentIndex = [[[arryaSegment objectAtIndex:indexPath.row]objectForKey:#"SelecteKey"] intValue];
return cell;
}
since you can't use the ugly way of getting the indexPath with the tag of the segmentedControl anymore, you have to use a method which gets the indexPath in a different way. Something like this:
- (IBAction)segSelected:(UISegmentedControl *)sender {
CGPoint originOfSegmentedControlInTableView = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:originOfSegmentedControlInTableView];
...
}
I have a table view in that I am using a custom cell. I have two buttons on each table view cell named "edit" and "cancel" both have images in cellForRow. What I want that when user clicks on edit button at same time same rows cancel button should change its image. Code is Working but it not changing same rows cancel button image. Its changing another rows cancel button image. How to maintain state of each button.
Here Is Code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
simpleTableIdentifier = #"MenuNameCell";
MenuNameCell *cell = (MenuNameCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell== nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MenuNameCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
NSLog(#"---------new cell agin");
}
else
[cell.contentView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
// Creating Label Menu Name
_nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 11, 82, 21)];
_nameLabel.backgroundColor = [UIColor clearColor];
_nameLabel.text = [_hotel._orderedMenus objectAtIndex:indexPath.row];
// Creating Label Menu Cost
_amountMenu = [[UILabel alloc] initWithFrame:CGRectMake(167, 13, 44, 21)];
_amountMenu.backgroundColor = [UIColor clearColor];
_amountMenu.text = [[_hotel._menuPrices objectAtIndex:indexPath.row] stringValue];
// Creating Text Field For Order Quantity
_textFieldQuantity = [[UITextField alloc] initWithFrame:CGRectMake(125,14,42,21)];
_textFieldQuantity.userInteractionEnabled = NO;
_textFieldQuantity.text = [[_hotel._selectedQuantity objectAtIndex:indexPath.row] stringValue];
//Creating Button For CANCEL Order
_cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_cancelButton setFrame:CGRectMake(265, 13, 25, 28)];
[_cancelButton setBackgroundImage:[UIImage imageNamed:#"cancel.png"] forState:UIControlStateNormal];
[_cancelButton setTag:indexPath.row];
[_cancelButton addTarget:self action:#selector(cancelOreder:) forControlEvents:UIControlEventTouchUpInside];
// Creating Button For Check Order
_checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_checkButton setFrame:CGRectMake(232, 13, 25, 28)];
[_checkButton setTag:indexPath.row];
[_checkButton setBackgroundImage:[UIImage imageNamed:#"edit.png"]forState:UIControlStateNormal];
[_checkButton addTarget:self action:#selector(editQuantity:) forControlEvents:UIControlEventTouchUpInside];
// Adding All To Content View
[cell.contentView addSubview:_nameLabel];
[cell.contentView addSubview:_amountMenu];
[cell.contentView addSubview:_textFieldQuantity];
[cell.contentView addSubview:_checkButton];
[cell.contentView addSubview:_cancelButton];
//objc_setAssociatedObject(_checkButton, iindex, indexPath,OBJC_ASSOCIATION_RETAIN );
return cell;
}
-(void)editQuantity:(id)sender{
NSLog(#"count of the array is----%d",[imageViewArray count]);
button = (UIButton *)sender;
row = button.tag;
NSLog(#"---rowww%d",row);
_textFieldQuantity.userInteractionEnabled = YES;
UIImage *buttonImage = [UIImage imageNamed:#"edit_over.png"];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
UIImage *buttonImageCancel = [UIImage imageNamed:#"check.png"];
[_cancelButton setBackgroundImage:buttonImageCancel forState:UIControlStateNormal];
_cancelButton.tag = 0;
}
-(void)cancelOreder:(id)sender{
button = (UIButton *)sender;
row = button.tag;
NSLog(#"The Row Selected iS At Cancel Order ISSSS----%d", row);
if (_cancelButton.tag == 0){
_textFieldQuantity.userInteractionEnabled = NO;
UIImage *buttonImageCancel = [UIImage imageNamed:#"check_over.png"];
[_cancelButton setBackgroundImage:buttonImageCancel forState:UIControlStateNormal];
UIImage *buttonImageCancel1 = [UIImage imageNamed:#"cancel.png"];
[_cancelButton setBackgroundImage:buttonImageCancel1 forState:UIControlStateNormal];
UIImage *buttonImage = [UIImage imageNamed:#"edit.png"];
[_checkButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
_cancelButton.tag = 1;
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"iHomeDelivery" message:#"Do You Want To Cancel the Order" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
Try to make _cancelButton and _checkButton with different tag in the same cell, I think it is puzzle to make both of them with same tag in the same row.
Give different tag for each buttons first like :
[_checkButton setTag:indexPath.row];
[_cancelButton setTag:indexPath.row+100];
and in your editQuantity: method
button = (UIButton *)sender;
row = button.tag;
NSIndexPath *currentIndexPath = [NSIndexPath indexPathForRow:row inSection:0];
UITableViewCell *newCell = [yourTable cellForRowAtIndexPath:currentIndexPath];
newCancelButton = (UIButton *)[newCell.contentView viewWithTag:row+100];
[newCancelButton setBackgroundImage:buttonImageCancel forState:UIControlStateNormal];
Do same in your cancelOreder: method to change image of _checkButton.
Also, use this for remove all subViews,
[cell.contentView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
please try this solution
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CELL_ID"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CELL_ID"];
UIButton *_cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[_cancelButton setFrame:CGRectMake(65, 13, 100, 28)];
[_cancelButton setTitle:#"Cancel" forState:UIControlStateNormal];
[_cancelButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[_cancelButton addTarget:self action:#selector(cancelOreder:) forControlEvents:UIControlEventTouchUpInside];
[_cancelButton setTag:1];
UIButton *_checkButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[_checkButton setFrame:CGRectMake(200, 13, 100, 28)];
[_checkButton setTitle:#"Check" forState:UIControlStateNormal];
[_checkButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[_checkButton addTarget:self action:#selector(editQuantity:) forControlEvents:UIControlEventTouchUpInside];
[_checkButton setTag:2];
[cell.contentView addSubview:_checkButton];
[cell.contentView addSubview:_cancelButton];
}
return cell;
}
-(void) cancelOreder:(id)sender
{
UITableViewCell *cell = (UITableViewCell *)[(UIButton *)sender superview];
UIButton *btn = (UIButton *)[cell viewWithTag:2];
if(btn != nil)
{
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
-(void) editQuantity:(id)sender
{
UITableViewCell *cell = (UITableViewCell *)[(UIButton *)sender superview];
UIButton *btn = (UIButton *)[cell viewWithTag:1];
if(btn != nil)
{
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
in this example i cheange text color of label you can change image,hope this will help you
I've added 3 labels and a button on cell of UITableView like this image
when i click on checkbox i change the label 'Due in 0 days' in some other stuff. it is ok with this code
[statusText setTextColor:[UIColor colorWithRed:0.0/255.0 green:51.0/255.0 blue:102.0/255.0 alpha:1.0]];
[statusText setFont:[UIFont boldSystemFontOfSize:16.0]];
statusText.tag = 100;
[statusText setBackgroundColor:[UIColor clearColor]];
[checkBoxBtn setBackgroundImage:[UIImage imageNamed:#"check_normal.png"] forState:UIControlStateNormal];
[checkBoxBtn addTarget:self action:#selector(showAlert:) forControlEvents:UIControlEventTouchUpInside];
checkBoxBtn.tag = indexPath.row;
[cell.contentView addSubview:checkBoxBtn];
this is my checkBoxAction and showAlert methods
-(void) checkBoxAction: (UIButton *)sender
{
NSInteger i =sender.tag + 1;
float perc = (i*100/18.0);
//NSLog(#"%.2f",perc);
NSString* percentageStr = [[NSString alloc] initWithFormat:#"%3.2f%%(%d out of 18)",perc, i];
barTitle.text = percentageStr;
barFGImage.hidden = NO;
if (perc == 100.00) {
[barFGImage setFrame:CGRectMake(barFGImage.frame.origin.x, barFGImage.frame.origin.y, 276.0, barFGImage.frame.size.height)];
}
else
[barFGImage setFrame:CGRectMake(barFGImage.frame.origin.x, barFGImage.frame.origin.y, 280*perc/100, barFGImage.frame.size.height)];
[sender setBackgroundImage:[UIImage imageNamed:#"check_select.png"] forState:UIControlStateNormal];
sender.userInteractionEnabled = NO;
NSString *currentDate = [formatter stringFromDate:[NSDate date]];
NSString* statusStr = [[NSString alloc] initWithFormat:#"Completed on %#",currentDate];
UILabel *tempLabel = (UILabel *)[[button superview] viewWithTag:100];
tempLabel.textColor = [UIColor colorWithRed:243.0/255.0 green:134.0/255.0 blue:48.0/255.0 alpha:1.0];
tempLabel.text = statusStr;
}
- (void) showAlert:(id)sender
{
button = (UIButton *)sender;
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"" message:#"Do you really want to complete this training?" delegate:self cancelButtonTitle:#"Yes, I've completed it" otherButtonTitles: #"No", nil];
[alert show];
}
Now i want that if i press checkBox on row (say)3rd then 'Due in 0 days' label of row 3rd, 4th and so on should changed.Any suggestion or sample code will be appreciated.
Thanks.
I am afraid my suggestion is not going to be a simple suggestion or code sample, but rather I would invite you to spend some time reading about MVC (Model View Controller).
From what I can tell from your code, your data seems to be 'stored' in your view objects. This is a very bad idea in general, this is a terrible idea with table views, as cells are being reused, and your data would just disappear as you scroll through large tables.
What you need to do is define model classes to hold your data, the controller objects will then access those model objects and display their data in the various views they control.
Once you implement this kind of pattern, checking something in one cell will update your model objects. Then all you will need to do is to make a simple [tableView reloadData]; call to update ALL your cells at once.
I have two buttons inside sectioned tableview cell thumbs up and thumbs down. Initially image of both button is not selected. When I select thumbs up button its image become thumbs up selected and other one become thumbsdown not selected and vice versa.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(#"mod:numberOfSectionsInTableView");
NSLog(#"[preferences count]=%d",[preferences count]);
return [preferences count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.choices count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexpath
{
NSLog(#"cellForRowAtIndexPath: DISPLAY TEST");
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
NSLog(#"Text is: %#", [choices objectAtIndex:indexpath.row]);
NSLog(#"CHOICE AT INDEX PATH IS: %#",[choices objectAtIndex:indexpath.row %[choices count]]);
cell.textColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor blackColor];
// Thumbs up button.
//UIButton *thumbsUp = [[UIButton alloc]init];
thumbsUp = [UIButton buttonWithType:UIButtonTypeCustom];
[thumbsUp setTag:(indexpath.row+(indexpath.section * 50))];
[thumbsUp addTarget:self action:#selector(thumbUp_ButtonClicked:event:)
forControlEvents:UIControlEventTouchUpInside];
[thumbsUp setTitle:#"" forState:UIControlStateNormal];
thumbsUp.frame = CGRectMake(150.0, 20, 20, 15);
[thumbsUp setBackgroundImage:[UIImage imageNamed:#"thumbsup_not_selected.png"]
forState:UIControlStateNormal];
//NSLog(#"------------------>TAG TEST : %d",(indexpath.row+(indexpath.section * 50)));
[cell.contentView addSubview:thumbsUp];
// Thumbs down button
thumbsDown = [UIButton buttonWithType:UIButtonTypeCustom];
[thumbsDown addTarget:self action:#selector(thumbDown_ButtonClicked:event:)
forControlEvents:UIControlEventTouchUpInside];
[thumbsDown setTitle:#"" forState:UIControlStateNormal];
thumbsDown.frame = CGRectMake(200, 20, 20, 15);
[thumbsDown setTag:indexpath.row+120];
[cell.contentView addSubview:thumbsDown];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[thumbsDown setBackgroundImage:[UIImage imageNamed:#"thumbsdown_not_selected.png"]
forState:UIControlStateNormal];
}
NSLog(#"------------> TAG TEST %d",thumbsUp.tag);
cell.text = [choices objectAtIndex:(indexpath.row % [choices count])];
NSLog(#"HELLO FOR TESTING");
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
// Create label with section title
UILabel *label = [[[UILabel alloc] init] autorelease];
label.frame = CGRectMake(15, 10, 300, 25);
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0.0, 1.0);
label.font = [UIFont boldSystemFontOfSize:16];
label.textAlignment = UITextAlignmentLeft;
label.text = sectionTitle;
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(12, 0, 300, 60)];
[view autorelease];
[view addSubview:label];
//[view addSubview:segmentedControl];
view.backgroundColor = [UIColor grayColor];
return view;
}
//Thumbs Up Button Action
- (IBAction)thumbUp_ButtonClicked:(id)sender event:(id)event
{
NSLog(#"Thumbs Up Check!");
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *) [[button superview] superview];
NSIndexPath *indexPath = [myTable indexPathForCell:cell];
NSLog(#"indexpath =%d",indexPath.row);
//[button setTag:indexPath.row+(indexPath.section * 50)];
int cTag = [sender tag];
NSLog(#"------>TAG : %d", cTag);
NSLog(#"------> Calculated TAG %d",indexPath.row+(indexPath.section * 50));
if(cTag == (indexPath.row+(indexPath.section * 50)))
{
NSLog(#"BUTTON COUNT:");
[button setBackgroundImage:[UIImage imageNamed:#"thumbsup_selected.png"]
forState:UIControlStateNormal];
}
NSInteger section = indexPath.section;
NSInteger row = indexPath.row;
//int row = button.tag;
NSLog(#"SECTION IS:%d",section);
NSLog(#"ROW IS: %d",row);
NSArray *array = cell.contentView.subviews;
NSLog(#"NUMBER OF OBJECTS: %d",[array count]);
UIButton *test = (UIButton *)[array objectAtIndex:2];
[test setBackgroundImage:[UIImage imageNamed:#"thumbsdown_not_selected.png"]forState:UIControlStateNormal];
}
Due to issue with tag of button while I change image of one button several buttons are changing. If any one can please find a solution it will be helpful.... tag is setting for buttons in sections which we can view.
The reason is the bad use of the recycling/reuse mechanism (as with 75% of questions about UITableView…)
Go read the Table View Programming Guide in Apple's doc (and search SO and the web for any question related to tableview and the reuse mechanism)
Corrected the issue by creating buttons outside and inside if(cell == nil). Also created a mutable dictionary to keep the current state of the button.....