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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:17:26+00:00 2026-05-23T06:17:26+00:00

How to properly add/remove button on right side (accessory view) of a cell? How

  • 0

How to properly add/remove button on right side (accessory view) of a cell?

How to add directory checking condition to each cell and then display one of 2 types of cell?

In my case: ViewWillAppear downloads XML with directory names and loads it to TableView. Each cell is checking if the directory exists on phone. If it does then the cell is without button (off), if it doesn’t the cell gets a button (on). Also ImageViews are different.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [uitableview dequeueReusableCellWithIdentifier:CellIdentifier];

    SomeClass *k = (SomeClass*)[self.arr_SomeClass objectAtIndex:indexPath.row];
    NSString *checkThisDir = [documents_dir stringByAppendingPathComponent:[k.dir_name stringByDeletingPathExtension]];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

        cell.imageView.tag = 121;
        cell.imageView.contentMode = UIViewContentModeScaleAspectFit;

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    if (![[NSFileManager defaultManager] fileExistsAtPath:dir_name isDirectory:nil]) {

        cell.imageView.image = [UIImage imageNamed:@"icon_on.png"];

        UIButton *bt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        bt.contentMode = UIViewContentModeScaleAspectFit;
        bt.tag = 123;
        UIImage *imgDownload = [UIImage imageNamed:@"icon_button.png"];
        [bt setImage:imgDownload forState:UIControlStateNormal];
        [bt setImage:imgDownload forState:UIControlStateHighlighted];
        [bt setImageEdgeInsets:UIEdgeInsetsMake(4,3,2,3)];
        [bt setFrame:CGRectMake(282,5,34,34)];
        [bt addTarget:self action:@selector(listButtonClick:event:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:bt];

        UIView *empty = [UIView new]; // makes space for button
        cell.accessoryView = empty;
        [empty release];
    }
    else {      

        // or: cell.imageView.image = [UIImage imageNamed:@"icon_off.png"];
        UIButton *bt = (UIButton*)[cell viewWithTag:123];
        [bt removeFromSuperview];

        UIImageView *iv = (UIImageView*)[cell viewWithTag:121];
        iv.image = [UIImage imageNamed:@"icon_off.png"];
    }
    cell.detailTextLabel.text = k.details;
    cell.textLabel.text = k.name;

    return cell;    
}

listButtonClicked simply downloads a file and creates directory and then does reloadData and reloadRowsAtIndexPaths on TableView.

Problem is that when I scroll the list high or low enough then sometimes there are buttons being added to cells that shouldn’t have them.

Any help would be nice, thanks in advance!

.

SOLVED:

Moved button creation to if (cell==nil) { .. } and outside added:

UIButton *bt = (UIButton*)[cell viewWithTag:123];
if (![[NSFileManager defaultManager] fileExistsAtPath:albumDir isDirectory:nil]) {
    cell.imageView.image = [UIImage imageNamed:@"icon_on.png"];
    [bt setHidden:NO];
}
else {
    cell.imageView.image = [UIImage imageNamed:@"icon_off.png"];
    [bt setHidden:YES];
}
  • 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-23T06:17:27+00:00Added an answer on May 23, 2026 at 6:17 am

    This happens because you are dequeuing the cells from the table (that’s good!), so that iOS does not recreate them every time you need a new cell. Once the cell gets dequeued it will bring inside all the subviews you added to it before (eg. for another row). For this whole thing to work correctly, once you have dequeued a cell, you should clean all its content and then render it as you wish.

    Otherwise, a better approach could be to create a cell in the if (cell == nil) block, automatically add the view for the image and the button, with no image set. Once you pass that block, you get the image using the tag ([cell viewWithTag:121]) and also the button ([cell viewWithTag:123]), and set them as hidden. From now on you can start defining the cell as required.

    This way, every time you request a cell, it will have a default image and button inside, but hidden, and later you set them as visible depending on your needs.

    This should solve your issue. Let me know if it helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a TableLayoutPanel with 3 columns and 1 row: (Remove button, User Control,
So, I'm trying to dynamically add a button to a site to give the
Have a script to add / remove options from a select field -- the
Basically I want to remove my subview when a button is pressed, here is
I need to add shortcodes such as image , hr and button . However,
I want to add a view from a nib file as a subview of
I'm trying to add a button to the OpenLayers map that is supposed to
I am trying to add the remove buttons dynamically from a jQuery UI Dialog,
I have just created add input field with <a id=add>Add</a> link button which is
I have a master-child tables. User can add/remove childs after selecting the master item/entity.

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.