开发者

Repeat Problem of tableview cell during scrolling in iphone

开发者 https://www.devze.com 2022-12-15 16:38 出处:网络
There are 5 cells and each cell has one dynamic label. the height of cell is defined as per label content using heightForRowAtIndexPath method. Now, 3开发者_运维知识库 cells are being displayed. When

There are 5 cells and each cell has one dynamic label. the height of cell is defined as per label content using heightForRowAtIndexPath method. Now, 3开发者_运维知识库 cells are being displayed. When I scroll for next cells then cellForRowAtIndexPath is called once and In the output any one (4 OR 5) cell is displayed correctly but another has 1st cell content(repeating). Is there any solution to fix this? cellForRowAtIndexPath method should be called 2 times. I have defined sections and rows correctly.


Remove the judgement

if (cell == nil)

That's good.I solve my problem now.Thank you very much.


You probably are not reusing the cells correctly. When you scroll, a previous cell is reused by the UITableView, but you don't change its contents correctly.

The code in cellForRowAtIndexPath should look similar to this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *kCustomCellID = @"CustomCellID";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCellID];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kCustomCellID] autorelease];
    }

    cell.textLabel.text = [self.dataArray objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [self.dateFormatter stringFromDate:[NSDate date]];

    return cell;
}

Does your look like this?


As you don't have a fixed size for each row, you won't be able to reuse them, except to present the same content again or some similar content.

My suggestion is to create a identifier for each row with different size.

You said that you have a switch to add the content based on the indexPath, so you should dequeue the reusable cells inside each case statement, based on its size.


Never remove if(cell == nil).

Instead clear the subviews which you have added on cell.

  if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault    reuseIdentifier:CellIdentifier] ;
       }else{
       NSArray *cellSubviews= [cell.contentView cellSubviews];
        for (UIView * view in subviews) {
            [view removeFromSuperview];
        }
    }


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell %d",indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

        cell.textLabel.text = @"Hello";

    }
    // Configure the cell.
    return cell;
}


Try and give a unique identifier for every cell,

NSString *CellIdentifier =[NSString stringWithFormat:"Cell %d",indexpath.row];

don't know if this is the right way, but it should solve ur problem.


The problem is that the cell is not nill so it returns same cell multiple times while scrolling. I removed the condition from cellForRowAtIndexPath method.

if (cell == nil)

Now each time, It is allocating cell and works fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消