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 7646379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:11:34+00:00 2026-05-31T10:11:34+00:00

I´m loading a bunch of countries to a table and allowing the user to

  • 0

I´m loading a bunch of countries to a table and allowing the user to delete. The rows directly correspond to the countries array, by using objectAtIndex:indexPath.row and I set the delete button´s tag to the same index.

When I delete the first couple of rows it´s ok but afterwards the wrong rows are getting deleted and the app crashes.

Here’s how I create rows:

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
NSString *currentText;

// fill in country cell content 
if ([tableView tag] == 400) {
    cell = [tableView dequeueReusableCellWithIdentifier:@"SettingsCountryCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                      reuseIdentifier:@"SettingsCountryCell"];
        NSLog(@"is nil"); 
    } // it´s never nil

    currentText = [arrSelectedCountries objectAtIndex:indexPath.row];
    int currentRow = indexPath.row;

    UILabel *countryLabel = (UILabel *)[cell viewWithTag:401];
    countryLabel.text = currentText;

    // create button
    UIButton *countryButton = (UIButton *)[cell viewWithTag:402];

    [countryButton setTag:currentRow];
    [countryButton setTitle:[NSString stringWithFormat:@"row%d", currentRow] 
                   forState:UIControlStateNormal];
    NSLog(@"%@ - tag %d - title %@ - button tag %d", 
          currentText, 
          currentRow, 
          countryButton.titleLabel.text,
          countryButton.tag);

    // on load: Bulgaria - tag 2 - title row2 - button tag 2
    // after one deletion (and ever after): 
    //      Bulgaria - tag 1 - title (null) - button tag 0 

    [countryButton addTarget:self 
                      action:@selector(removeCountry:)
            forControlEvents:UIControlEventTouchUpInside]; 

}    

Here’s how I remove them (i.e. countries):

- (void)removeCountry:(UIButton *)countryButton
{
// removed country
NSString *currentText = [arrSelectedCountries objectAtIndex:countryButton.tag];

NSLog(@"removed country %@ at tag %d", currentText, countryButton.tag);

// ok: removed country All CEE Countries at tag 0
// ok: removed country Bulgaria at tag 0
// not ok: removed country Czech Republic at tag 1

// add to picker selection and remove from selected
[arrCountries addObject:currentText];
[arrSelectedCountries removeObject:currentText];

// refresh picker and country table
[countryPicker reloadAllComponents];

[countryTable reloadData];

[countryTable reloadSections:[NSIndexSet indexSetWithIndex:0] 
            withRowAnimation:UITableViewRowAnimationFade];

// position table and elements below it
[self repositionForCountryChange]; 
}

At first the table is showing: row0, row1, row2, etc.

After a couple of deletions: row1, row2, row3, etc.

Once this happens, the label is no longer corresponding properly to the tag. When I click delete on Croatia, delete button labeled row 1, NSLog says:

removed country Czech Republic at tag 1

… and Croatia row is not deleted although its delete button now says row2.

And why am I getting from NSLog:

title (null) - button tag 0 

…when I can see in the table that there is a title and removeCountry can access button tag, although incorrectly?

  • 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-05-31T10:11:36+00:00Added an answer on May 31, 2026 at 10:11 am

    You have set up the button with a tag to allow you to identify it as a subview in the cell (402) you then change the tag so it identifies the row the tag is in. When the cell is re-used, there is no view with tag 402, so your button is still identified with a tag number of its original row.

    You’re using tags for two purposes. Ideally, don’t use them at all as they are fragile and do not make your code readable.

    • create a UITableViewCell subclass and identify its subviews using outlets
    • identify the row of the pressed button using the method I describe here which is much more flexible than tags and keeps working if you delete rows.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using NHibernate with NHibernate.Linq, and have a bunch of dynamically loading modules
I am loading a bunch of images using knockoutjs. I get a json object
i'm loading bunch of images in coverflow using i carousel but i'm not getting
I am loading a bunch of rss feeds using DOM and sometimes one will
I have a page where I am loading bunch of images initially when the
I am loading in a bunch of data that contains dates formatted as shown
I have a bunch of thumbnails which I am loading with a style of
I've seen a bunch of articles relating to loading the entire Rails environment for
After loading a PHP template (using jQuery's load function), this simple script won't make
I want to read a bunch of text files, by loading them as resources

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.