45452

Table view return wrong values when scrolling

Question:

The problem is that I have implemented a table view with custom cell from storyboard. Each cell has a button as a selection button which mean whenever I press on a button , it image change. For example, if I press on the button with cell index=0, it image change correctly however when scrolling the table I found other buttons also changed their images! The problem is from the table index path and I spent a lot of time trying to fix it with no result. Any Solution?

<h2>Thank you</h2> static NSString *simpleTableIdentifier = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier]; NSLog(@"index path inside cell = nil %i",(int)indexPath); } buttonObj=(UIButton *)[cell viewWithTag:40]; [buttonObj addTarget:self action:@selector(select:) forControlEvents:UIControlEventTouchDown]; return cell; -(void) select:(id)sender{ UIButton *button=(UIButton *)sender; UITableViewCell *clickedCell = (UITableViewCell*)[[sender superview] superview]; NSIndexPath *indexPathCell = [addFriendTableView indexPathForCell:clickedCell]; NSLog(@"indexPathCell %i",(int)indexPathCell.row); Contacts* selectedContact = [contactsArray objectAtIndex:indexPathCell.row]; if([self image:button.imageView.image isEqualTo:[UIImage imageNamed:@"addFriend.png"]]==YES){ [button setImage:[UIImage imageNamed:@"addFriendPressed.png"] forState(UIControlStateNormal)]; [selectFriendsArray addObject:selectedContact]; counter++ } else if( [self image:button.imageView.image isEqualTo:[UIImage imageNamed:@"addFriendPressed.png"]]==YES) { [button setImage:[UIImage imageNamed:@"addFriend.png"] forState:(UIControlStateNormal)]; counter--; [selectFriendsArray removeObject:selectedContact]; }

Answer1:

Here what you are doing is getting same UIButton object for every cell. You need to create a separate button for every cell.

Identify this UIButton object with unique tag and then select that cell for further.

Create a tag for UIButton object. Put below line at top of your view controller class after #import statments

#define kButtonTag 1000

Now set unique tag for each object.

static NSString *simpleTableIdentifier = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier]; NSLog(@"index path inside cell = nil %i",(int)indexPath); } // kButtonTag = 1000 NSInteger btnTag = kButtonTag + indexPath.row; [[cell.contentView viewWithTag:btnTag] removeFromSuperview]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; // Set button properties which you required here... //.... [button setTag:[cell viewWithTag:kButtonTag + indexPath.row]]; [button addTarget:self action:@selector(select:) forControlEvents:UIControlEventTouchDown]; [cell.contentView addSubview:button];

In your select method, check for that button tag.

- (void) select:(UIButton *)sender { NSInteger tag = sender.tag; NSInteger index = tag - kButtonTag; // Above index is unique index for your cell. }

Hope you will get idea, From this short explanation.

Answer2:

This is down to cell reuse. When cells scroll off screen, they are put into a queue to be reused as other cells appear on screen. The dequeue method pulls them off this queue. Your problem is that the button image does not get reset when this happens, so if it had previously been changed, it wiil remain.

You need to add some code to your cellForRowAtIndexPath method to set the correct image based on its row.

Recommend

  • How to access variables within a subview of UITableViewCell contentView in iOS Swift Project
  • Allowing user to select a UIPickerView row by tapping
  • Adding a button inside a popover
  • How to add cell dynamically in uitableviewcontroller?
  • UITextField keyboard can't type anything
  • NSUserDefaults not working with UITableView in Swift
  • how to check the condition for UIButtons in UITableview
  • How to add next and previous buttons programmatically in iPhone?
  • Label blocking a button click
  • LandScape/Portrait size differ iOS
  • clicking button within UITableview cell gives wrong result while search
  • pushViewController from another class
  • Swift, collection view & storyboard, get null when using viewWithTag on another Label
  • How to track where the right callout accessory view is when it was tapped on
  • Using UIButton to navigate between views
  • AudioUnit framework not found
  • Objective-C: Tabbaritem tapped->Method Called-> But WebView not refreshed
  • UIRefreshControl with low height UICollectionView
  • Why direct memory 'array' is slower to clear than a usual Java array?
  • #selector' refers to a method that is not exposed to Objective-C swift 3
  • change backbutton with custom image and textcolor
  • Custom UITableViewCell in UISearchDisplayController
  • ios - navigationItem.titleView blinks when updated
  • How to get rid of gap around Round Rect Button without removing border around it? [duplicate]
  • Text overlay not showing in GPUImage iOS
  • UItableView Replacing Checkmarks with UIButton
  • Putting text shadow on a UIButton text label
  • Argument of '#selector' does not refer to an '@objc' method (swift 3)
  • How to remove duplicate buttons
  • Passing argument within action selection in UIButton
  • PHP problems with current url
  • Get UILabel out of UIButton
  • Aligning image on right side of title on UIButton with imageEdgeInsets and titleEdgeInsets
  • redirect_to root_url and return unless @user.activated
  • Getting the scrolling offset when storing coordinates
  • Bootstrap (v3.3.4) glyphicons not displayed in IE when refresh page (F5)
  • Implementing “partial void” in VB
  • Sending HTML Form Data to Spring REST Web Service
  • How do I superscript characters in a UIButton?
  • what is the difference between the asp.net mvc application and asp.net web application