Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8931521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:07:53+00:00 2026-06-15T09:07:53+00:00

I have a weird problem going on here. I have a TableView and I

  • 0

I have a weird problem going on here. I have a TableView and I assign the indexPath.row to the tag of a UIButton so I can pass that tag as a parameter on a segue and give an object to the next view controller. Everything works hunky dory until I go past 6 cells in the TableView, when pressing the button passes the wrong object from the array. I decided to put in some NSLogs to see what was happening and I find the results quite bizarre. The assignment buttonForApplyingTag.tag = indexPath.row; happens just after the NSLogs and is the last statement before return cell;. Since the actual value of indexPath.row is correct, the objects from the array are getting loaded properly. It’s just the buttons that are causing problems. Here is my output:

2012-11-28 19:01:57.596  IndexPath.row: 0
2012-11-28 19:01:57.596  IndexPath.Row from Tag: 0
2012-11-28 19:01:57.597  SearchResults Count: 100
2012-11-28 19:01:57.598  IndexPath.row: 1
2012-11-28 19:01:57.598  IndexPath.Row from Tag: 1
2012-11-28 19:01:57.598  SearchResults Count: 100
2012-11-28 19:01:57.599  IndexPath.row: 2
2012-11-28 19:01:57.600  IndexPath.Row from Tag: 2
2012-11-28 19:01:57.600  SearchResults Count: 100
2012-11-28 19:01:57.601  IndexPath.row: 3
2012-11-28 19:01:57.601  IndexPath.Row from Tag: 3
2012-11-28 19:01:57.602  SearchResults Count: 100
2012-11-28 19:01:57.602  IndexPath.row: 4
2012-11-28 19:01:57.603  IndexPath.Row from Tag: 4
2012-11-28 19:01:57.603  SearchResults Count: 100
2012-11-28 19:01:57.604  IndexPath.row: 5
2012-11-28 19:01:57.605  IndexPath.Row from Tag: 5
2012-11-28 19:01:57.605  SearchResults Count: 100
2012-11-28 19:02:02.004  IndexPath.row: 6
2012-11-28 19:02:02.004  IndexPath.Row from Tag: 6
2012-11-28 19:02:02.005  SearchResults Count: 100
2012-11-28 19:02:03.993  IndexPath.row: 7
2012-11-28 19:02:03.993  IndexPath.Row from Tag: 0
2012-11-28 19:02:03.993  SearchResults Count: 100
2012-11-28 19:02:17.846  IndexPath.row: 8
2012-11-28 19:02:17.846  IndexPath.Row from Tag: 0
2012-11-28 19:02:17.846  SearchResults Count: 100
2012-11-28 19:02:18.482  IndexPath.row: 9
2012-11-28 19:02:18.482  IndexPath.Row from Tag: 0
2012-11-28 19:02:18.482  SearchResults Count: 100

Sorry for the excessive amount of lines, but you get the idea. This trend continues all the way down to 100, meaning that the buttons are not receiving the correct tag. Even weirder is the fact that when the button is clicked on the cells with a false indexPath.row tag, another tag is apparently made up and I get a low value tag. Furthermore, when the cell 6 has partially appeared at the bottom of the TableView, even though it’s clear the cell has actually loaded, the NSLogs haven’t been posted to the debugger, and therefore the button fails and returns a tag of 5. So there are actually 3 issues with this way of doing things.

Really, what I want to ask is why is this strange behaviour happening, and how can I fix it? And secondly, since this just appears to be a disaster, is there a better way of passing down the indexPath.row value so I can get an object from my array when the button is clicked?

Thanks for any help.
Regards,
Mike

P.S. I’m a complete amateur so if you could explain it in Lehmann’s terms, I’d appreciate it.

EDIT: Here’s the code where I actually assign the tag to the button, as requested.

    UIButton *buttonForApplyingTag = (UIButton *)[cell viewWithTag:1004];

    NSLog(@"IndexPath.row: %d", indexPath.row);
    NSLog(@"IndexPath.Row from Tag: %d", buttonForApplyingTag.tag);
    NSLog(@"SearchResults Count: %d", [searchResults count]);
    buttonForApplyingTag.tag = indexPath.row;

    return cell;

EDIT 2: Here’s the entire cellForRowAtIndexPath code:

if (isLoading) {
    return [tableView dequeueReusableCellWithIdentifier:LoadingCellIdentifier];
} else if ([searchResults count] == 0) {
    return [tableView dequeueReusableCellWithIdentifier:NothingFoundCellIdentifier];
} else {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SearchResultCellIdentifier];
    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [tableView setSeparatorColor:[UIColor grayColor]];
    /* Then we do a load of assignments for labels in the cell, then we reach the tagging code. */

SearchResultCellIdentifier comes from this: static NSString *const SearchResultCellIdentifier = @"SearchResultCellProto";

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:07:54+00:00Added an answer on June 15, 2026 at 9:07 am

    It looks like what you want to do is have buttons in cells, and then know which cell’s button was pressed when the button action occurs. Using tags is a reasonable idea, but tough to implement because the tag needs to change when the cell gets reused (and ends up at a different index path).

    Here’s a better approach: Add the buttons using a constant tag (or no tags), then on the click action do this…

    - (IBAction)pressedButtonInMyCell:(id)sender {
    
        UIButton *button = (UIButton *)sender;
    
        // find the cell that contains the button, this might be one or two levels
        // up depending on how you created the button (one level in code, two in IB, probably)
        UIView *view = button.superview;
        while (view && ![view isKindOfClass:[UITableViewCell self]]) view = view.superview;
    
        UITableViewCell *cell = (UITableViewCell *)view;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
        NSLog(@"cell is in section %d, row %d", indexPath.section, indexPath.row);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a weird problem that i can't figure out a solution for: I've
I have got a weird problem here, and more interesting that it works fine
I have a bit of a weird problem here! I am trying to write
Really weird authentication problem going on - hope someone can help! The Domino Web
I have a weird problem in some JavaScript code that's a part of one
I have a really weird WCF problem here... We're connecting to a crappy third-party
I have a weird problem with a Java Gregorian Calendar: SimpleDateFormat sdf = new
I have a weird problem. When I execute a query the data is not
I have a weird problem, so I thought I would ask and see if
I have a weird problem. I create this window, but its blank until i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.