Repeat tableview cell issue while scrolling on iphone

There are 5 cells, and each cell has one dynamic label. cell height is defined as the contents of the label using the heightForRowAtIndexPath method. Now 3 cells are displayed. When I scroll for the following cells, cellForRowAtIndexPath is called once, and at the output any cell (4 OR 5) is displayed correctly, and the other has the 1st cell content (repeated). Is there any solution to fix this? The cellForRowAtIndexPath method should be called 2 times. I correctly defined sections and lines.

+2
source share
7 answers

Clear judgment

if (cell == nil)

It's good. I am solving my problem now. Thank you very much.

+3
source

, . , UITableView, .

cellForRowAtIndexPath :

- (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;
}

?

+1

, , , - .

- .

, indexPath, case .

0

if(cell == nil).

, .

  if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault    reuseIdentifier:CellIdentifier] ;
       }else{
       NSArray *cellSubviews= [cell.contentView cellSubviews];
        for (UIView * view in subviews) {
            [view removeFromSuperview];
        }
    }
0
- (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;
}
0

,

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

, , .

0

, , . cellForRowAtIndexPath.

if (cell == nil)

Now every time it selects a cell and works fine.

-6
source

All Articles