Issue with button tag in sectioned UITableView?

This is a common occurrence due to the fact that the table view cells are recycled. Your scheme with the button tags is rather convoluted and error prone.

Up vote -4 down vote favorite share g+ share fb share tw.

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 { enter code hereNSLog(@"mod:numberOfSectionsInTableView"); NSLog(@"preferences count=%d",preferences count); return preferences count; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {enter code here enter code herereturn 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 allocinit; 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.... iphone objective-c ios uitableview iphone-sdk-4.3 link|improve this question edited Oct 29 '11 at 14:04 asked Oct 28 '11 at 12:13Susha194 100% accept rate.

This is a common occurrence due to the fact that the table view cells are recycled. Your scheme with the button tags is rather convoluted and error prone. It would make more sense to keep track of the ThumbsUp/Down state of each cell in your table view's datatsource data model.

Then, in cellForRowAtIndexPath: set each button state explicitly. BTW: cell. Text and cell.

TextColor are deprecated. You should use the properties of cell.textLabel.

1 for figuring out the problem from the question :-) – Owen Hartnett Oct 29 '11 at 4:17 If any one can please provide code to rectify the issue it will be helpful. Problem is with setting tag for button... – Susha Oct 29 '11 at 10:40.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions