Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Tableview with custom cell scrolling problem

jsantojsanto Posts: 30New Users @
I know this is a very well known problem all over the web, and i have search but i´m still having problems when scrolling my tableview, so i was hopping someone could point me in the right direction:

I have made a custom cell which has several labels and one of them is a title for it. Because the title is suppose to change depending on the data that is loaded, the cell not always has the same amount of labels or height or position of the labels. I´m controlling the amount of labels (set hidden) and position of labels (set frame) in my custom cell class and for the height i´m using tableView heightForRowAtIndexPath. When i load the tableview everything is in the right position, but when i scroll the labels appeared disorganized and sometimes they just don´t appear...here is my code:

In my tableview:
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@ThreeLinesDesign bundle:nil] forCellReuseIdentifier:@threeLines];

    (in here i have the code that is filling the 3 arrays below firstsection etc..)

    tableContent = [[NSMutableArray alloc]initWithObjects:firstSection,secondSection,thirdSection, nil];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger sections = [[self tableContent] count];
    return sections;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    sectionContents = [[self tableContent] objectAtIndex:section];
    NSInteger rows = [sectionContents count];
    return rows;
}

From what i read, the problem normally is in the cellForRowAtIndexPath, my problem is in here: (i think)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = Nil;
    ...
    else if (indexPath.section ==1)
    {
        contentForRow = [sectionContents objectAtIndex:[indexPath row]];

        CellThreeLines *cell = [tableView dequeueReusableCellWithIdentifier:@threeLines];

        if (cell == nil)
        {
            cell = [[CellThreeLines alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@threeLines];
        }
        [cell.cellName setText:contentForRow];
        [cell.montProd setText:@Mont];
        [cell.prod setText:@Prod];
        [cell.acab setText:@Acab];
        [cell cellResults];// this is a method in custom cell (and it´s big) and it makes all the mathematical calculations that i want to display later in the labels.
   }
   ...
}

The tableview can have a maximum of 20 cells...basically, the cell is always the same but with different configuration and the content of the labels is core data...is it a memory issue? should i be doing a specific custom cell for each title?

Sorry for being so specific but maybe the problem is something else than cellforrowatindexpath and i´m not seeing it.

Can anybody help me?

Thanks in advance.

Replies

  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,677Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    Well, mostly you aren't being specific enough.

    My guess is that you aren't properly taking into account that the cell could be dirty. For example, if all your code does is hide labels, then eventually all of your labels will be hidden as cells get reused. Somewhere, you need to show the labels, too. Every time you get a cell, you have to assume it is wrong, and take the necessary steps to make it right. Show OR hide labels, as appropriate. Set OR clear text contents, as appropriate. Etc.

    You might also consider making separate cell classes. Sounds like you have made this one too complicated.

    And this:
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        sectionContents = [[self tableContent] objectAtIndex:section];
    

    There is NO reason for sectionContents to be an instance variable. None. This is wrong.

    And use properties.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
Sign In or Register to comment.