-1

When I select buttons within these UITableViewCells, the button sometimes passes data from the wrong row. In this table view, there are 10 sections, each with a section header and with only 1 row. The row height takes up most of the screen, as there are 6 buttons within it. Each UIButton, when selected, opens a new viewcontroller with an enlarged view of the button's image (removed this code to make it more readable here).

Here is the problem: each button successfully pushes a modal viewcontroller; however, as I scroll down, sometimes the buttons pass the data from the same button from the section below it (e.g. button1 in section 4 when clicked may pass the data for button1 in section 5). Similarly, as I scroll back up, sometimes the buttons pass the data from the same button from the section above it (e.g. button5 in section 7 when clicked may pass the data for button5 in section 6).

I think this problem occurs because the iPhone is pulling the most recently loaded cell (when cellForRowAtIndexPath is called), but I would like it to pass the data for the row in which the button is located.

Thoughts? Thanks in advance for any help you can provide.

Edit: Added data code as requested

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

// dataDict pulled from plist    
NSDictionary *button1Dict = [dataDict objectForKey:@"button1"];
NSDictionary *button2Dict = [dataDict objectForKey:@"button2"];
NSDictionary *button3Dict = [dataDict objectForKey:@"button3"];
NSDictionary *button4Dict = [dataDict objectForKey:@"button4"];
NSDictionary *button5Dict = [dataDict objectForKey:@"button5"];
NSDictionary *button6Dict = [dataDict objectForKey:@"button6"];

// selectedCatTag and selectedItemTag for each button are alloc and init in viewDidLoad
button1SelectedCatTag = (NSUInteger *)[[button1Dict objectForKey:@"selectedCatTag"] integerValue];
button2SelectedCatTag = (NSUInteger *)[[button2Dict objectForKey:@"selectedCatTag"] integerValue];
button3SelectedCatTag = (NSUInteger *)[[button3Dict objectForKey:@"selectedCatTag"] integerValue];
button4SelectedCatTag = (NSUInteger *)[[button4Dict objectForKey:@"selectedCatTag"] integerValue];
button5SelectedCatTag = (NSUInteger *)[[button5Dict objectForKey:@"selectedCatTag"] integerValue];
button6SelectedCatTag = (NSUInteger *)[[button6Dict objectForKey:@"selectedCatTag"] integerValue];

button1SelectedItemTag = (NSUInteger *)[[button1Dict objectForKey:@"selectedItemTag"] integerValue];
button2SelectedItemTag = (NSUInteger *)[[button2Dict objectForKey:@"selectedItemTag"] integerValue];
button3SelectedItemTag = (NSUInteger *)[[button3Dict objectForKey:@"selectedItemTag"] integerValue];
button4SelectedItemTag = (NSUInteger *)[[button4Dict objectForKey:@"selectedItemTag"] integerValue];
button5SelectedItemTag = (NSUInteger *)[[button5Dict objectForKey:@"selectedItemTag"] integerValue];
button6SelectedItemTag = (NSUInteger *)[[button6Dict objectForKey:@"selectedItemTag"] integerValue];

UIButton *button1 = (UIButton *)[cell viewWithTag:1];
UIButton *button2 = (UIButton *)[cell viewWithTag:2];
UIButton *button3 = (UIButton *)[cell viewWithTag:3];
UIButton *button4 = (UIButton *)[cell viewWithTag:4];
UIButton *button5 = (UIButton *)[cell viewWithTag:5];
UIButton *button6 = (UIButton *)[cell viewWithTag:6];

CGFloat kImageSquareSideLength = 100.0;

if (button1Dict) {
    NSDictionary *selectedCatDict = [dataCategories objectAtIndex:(int)button1SelectedCatTag];
    NSString *imageName = [[selectedCatDict objectForKey:@"itemImages"] objectAtIndex:(NSUInteger)button1SelectedItemTag];
    [button1 setImage:[[UIImage imageNamed:imageName] imageByScalingAndCroppingForSize:CGSizeMake(kImageSquareSideLength, kImageSquareSideLength)] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(button1Pressed:) forControlEvents:UIControlEventTouchUpInside];
}
if (button2Dict) {
    NSDictionary *selectedCatDict = [dataCategories objectAtIndex:(int)button2SelectedCatTag];
    NSString *imageName = [[selectedCatDict objectForKey:@"itemImages"] objectAtIndex:(NSUInteger)button2SelectedItemTag];
    [button2 setImage:[[UIImage imageNamed:imageName] imageByScalingAndCroppingForSize:CGSizeMake(kImageSquareSideLength, kImageSquareSideLength)] forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(button2Pressed:) forControlEvents:UIControlEventTouchUpInside];
}
if (button3Dict) {
    NSDictionary *selectedCatDict = [dataCategories objectAtIndex:(int)button3SelectedCatTag];
    NSString *imageName = [[selectedCatDict objectForKey:@"itemImages"] objectAtIndex:(NSUInteger)button3SelectedItemTag];
    [button3 setImage:[[UIImage imageNamed:imageName] imageByScalingAndCroppingForSize:CGSizeMake(kImageSquareSideLength, kImageSquareSideLength)] forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(button3Pressed:) forControlEvents:UIControlEventTouchUpInside];
}
if (button4Dict) {
    NSDictionary *selectedCatDict = [dataCategories objectAtIndex:(int)button4SelectedCatTag];
    NSString *imageName = [[selectedCatDict objectForKey:@"itemImages"] objectAtIndex:(NSUInteger)button4SelectedItemTag];
    [button4 setImage:[[UIImage imageNamed:imageName] imageByScalingAndCroppingForSize:CGSizeMake(kImageSquareSideLength, kImageSquareSideLength)] forState:UIControlStateNormal];
    [button4 addTarget:self action:@selector(button4Pressed:) forControlEvents:UIControlEventTouchUpInside];
}
if (button5Dict) {
    NSDictionary *selectedCatDict = [dataCategories objectAtIndex:(int)button5SelectedCatTag];
    NSString *imageName = [[selectedCatDict objectForKey:@"itemImages"] objectAtIndex:(NSUInteger)button5SelectedItemTag];
    [button5 setImage:[[UIImage imageNamed:imageName] imageByScalingAndCroppingForSize:CGSizeMake(kImageSquareSideLength, kImageSquareSideLength)] forState:UIControlStateNormal];
    [button5 addTarget:self action:@selector(button5Pressed:) forControlEvents:UIControlEventTouchUpInside];
} 

if (button6Dict) {
    NSDictionary *selectedCatDict = [dataCategories objectAtIndex:(int)button6SelectedCatTag];
    NSString *imageName = [[selectedCatDict objectForKey:@"itemImages"] objectAtIndex:(NSUInteger)button6SelectedItemTag];
    [button6 setImage:[[UIImage imageNamed:imageName] imageByScalingAndCroppingForSize:CGSizeMake(kImageSquareSideLength, kImageSquareSideLength)] forState:UIControlStateNormal];
    [button6 addTarget:self action:@selector(button6Pressed:) forControlEvents:UIControlEventTouchUpInside];
} 

return cell;

}

//all other button actions are identical to button1Pressed
- (IBAction)button1Pressed:(id)sender
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
SelectedItemViewController *destVC = [storyboard instantiateViewControllerWithIdentifier:@"SelectedItemViewController"];

destVC.selectedOutfitDict = @"DiscoverOutfits";
destVC.selectedCatTag = (int *)button1SelectedCatTag;
destVC.selectedItemTag = (int *)button1SelectedItemTag;

[self presentViewController:destVC animated:NO completion:nil];
}
  • add code to pass item data. I think that is where the problem lies – Pratyusha Terli Jan 23 '13 at 06:08
  • http://stackoverflow.com/questions/1802707/detecting-which-uibutton-was-pressed-in-a-uitableview?rq=1 – junaidsidhu Jan 23 '13 at 07:02
  • so you want to present the same viewcontroller for each button action? – DD_ Jan 23 '13 at 07:13
  • @PratyushaTerli I don't think the problem is with the data, but I've posted it in case you are correct. – user1978374 Jan 23 '13 at 16:03
  • @JayD I have tried similar code (using [sender superview] to get the indexPath), but the problem still remains. – user1978374 Jan 23 '13 at 16:04
  • @Dpk I am presenting the same view controller (an enlarged version of the selected button image) but with different data (each button has a different image). The problem is that the wrong data is sometimes passed. Each section has 1 row, which each has its own set of button1, button2, button3, button4, button5, and button6. The image of button1 in section 1 is different from the image of button1 in section 2, etc. – user1978374 Jan 23 '13 at 16:06
  • @user1978374: is my answer working?? – Neelam Verma Jan 24 '13 at 05:10

2 Answers2

1

yes you can get particular cell from which button is taped. write in your IBAction method and you will get particular cell taped button in indexPath object

UIView *senderButton = (UIView*) sender;
       NSIndexPath *indexPath = [(UITableView *)self.superview indexPathForCell: (UITableViewCell*)[[senderButton superview]superview]];

hope this will help

Bhavesh Lathigara
  • 1,355
  • 1
  • 15
  • 25
  • I deleted my earlier comments to avoid confusion for future readers. I realized my error, and your answer was closest to it-- I was setting the data for each button in cellForRowAtIndexPath! Of course the data passed was for the most recently loaded cell. The obvious solution is to set the data for each button when the cell is pressed. Everyone, thank you for your patience and help. – user1978374 Jan 25 '13 at 02:58
1

Sometimes its tagging problem.

Change the tag like this:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

   UIButton *button1 = [[UIButton alloc] init];
   button1.tag = indexPath.section +  indexPath.row + 8956;// 8956 is some random weird     number
  [cell.contentView addSubview:button1];
  [button1 release];

// same code for other buttons

}

UIButton *button1 = (UIButton *)[cell.contentView viewWithTag: indexPath.section +  indexPath.row + 8956];
UIButton *button2 = (UIButton *)[cell.contentView viewWithTag: indexPath.section + indexPath.row +8956];
UIButton *button3 = (UIButton *)[cell.contentView viewWithTag: indexPath.section + indexPath.row +8956];
UIButton *button4 = (UIButton *)[cell.contentView viewWithTag: indexPath.section + indexPath.row +8956];
UIButton *button5 = (UIButton *)[cell.contentView viewWithTag: indexPath.section + indexPath.row +8956];
UIButton *button6 = (UIButton *)[cell.contentView viewWithTag: indexPath.section + indexPath.row +8956];

Working??

Neelam Verma
  • 3,232
  • 1
  • 22
  • 33
  • @Neelz - this solution didn't work because there are 6 buttons within each cell (so with these tags we wouldn't be able to differentiate between different buttons when passing data to the next view controller). The solution is to set the button data in the buttonPressed methods -- a rather mindless error -- you have my sincere thanks for your help. – user1978374 Jan 25 '13 at 03:01
  • @user1978374: ya got it some tags are same... then we should make tags (some mathematics) so that they will not repeat. Thx – Neelam Verma Jan 25 '13 at 07:34