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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:01:07+00:00 2026-05-28T13:01:07+00:00

Below is code for UITableView , But when i scroll its behaves weirdly (too

  • 0

Below is code for UITableView, But when i scroll its behaves weirdly (too annoying)… This problem is due to reuseIdentifier…. but dont know how to solve..

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

    UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];

    NSInteger imgTag = 1;
    NSInteger lblTag = 2;

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(2, 2, 52, 52)];
//        Image:[UIImage imageNamed:[self.glassType objectAtIndex:indexPath.row]]];
        imgView.tag = imgTag;
        [cell.contentView addSubview:imgView];
        [imgView release];

        UILabel *lblName = [[UILabel alloc] initWithFrame:CGRectMake(60, cell.frame.size.height/4, 200, 21)];
//        lblName.text = [self.glassName objectAtIndex:indexPath.row];
        lblName.tag = lblTag;
        [cell addSubview:lblName];
        [lblName release];
    }

    NSInteger imgIndex = 2;
    NSInteger lblIndex = 3;

    ((UIImageView *)[cell viewWithTag:imgTag]).image = [[self.glassType objectAtIndex:indexPath.row] objectAtIndex:imgIndex];
    ((UILabel *)[cell viewWithTag:lblTag]).text = [[self.glassName objectAtIndex:indexPath.row] objectAtIndex:lblIndex];

    return cell;
}

- (void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView1 cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

How to make Cell for row at index so that it remains constant even when scrolled??? Also how to make single selection in UITableView??

  • 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-28T13:01:07+00:00Added an answer on May 28, 2026 at 1:01 pm

    The answer is that you should not add subviews to your table cells outside of the “if (cell == nil) { …” clause or they get added over and over again to the same cell when it gets re-used.

    See my answer to this question for a more detailed explanation, including code for how to fix it:

    cellForRowAtIndexPath memory management

    You also cannot store state in table cells because as soon as they scroll offscreen they are recycled and re-appear at a different index in your table. You need to set up an array of model objects to store state for your table cells (such as what their accessory type should be). A more detailed explanation can be found in my answer to this question:

    Looping through UITableViewCells of a UITableView

    If you fix how you are adding subviews to the cells, and store your “ticked” state in an array of model objects as well as setting the cell.accessoryType (so that it can be restored when the cell is dequeued), then your approach to row selection is otherwise correct.

    So put this in your tableView:cellForRowAtIndexPath: method, just before the return cell;:

    MyModelObject *object = [self.arrayOfModelObjects objectAtIndex:indexPath.row];
    BOOL isChecked = object.checked;
    cell.accessoryType = isChecked? UITableViewCellAccessoryCheckmark: UITableViewCellAccessoryNone;
    

    And in your tableView: didSelectRowAtIndexPath: method, get rid of the current logic and replace it with:

    - (void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath
        for (int i = 0; i < [self.arrayOfModelObjects count]; i++)
        {
            MyModelObject *object = [self.arrayOfModelObjects objectAtIndex:i];
            object.checked = (i == indexPath.row); // only check the one we just tapped
        }
    
        //refresh table to update the accessory views for all rows
        [tableView1 reloadData];
    }
    

    Obviously replace the arrayOfModelObjects with your own model implementation. You could just use an array of NSNumber objects containing bools if you don’t want to create a custom class for this purpose.

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

Sidebar

Related Questions

I am using the code below to put together my UITableView. The Cell height
i am using below code for auto scrolling UITableview - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return
I am getting allocation count plus one in the below code. - (UITableViewCell *)tableView:(UITableView
I have a UITableView which loads data from XML using the below code. It
I'm using the code below in the root view controller to hide the UITableView's
The code below does what I need it to do, but it looks like
I am using the below code; it works but the text appears with few
I am using the following code to edit a uitableview, but i'd like only
I'm using the code below in my view controller but the background image is
Here is my code below: - (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSInteger row

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.