开发者

UITableViewCell - view showing data on top of data when being reused

开发者 https://www.devze.com 2023-03-11 14:41 出处:网络
I\'m using a custom UITableViewCell. They load up great, but when they get reused instead of replacing the text in the labels, they are some how writing over the top of them. Any ideas how to stop thi

I'm using a custom UITableViewCell. They load up great, but when they get reused instead of replacing the text in the labels, they are some how writing over the top of them. Any ideas how to stop this behaviour?开发者_运维问答

I assume I'm not reseting the view correctly before it gets reused. I'm currently empty the labels so they have just a blank @"" string. But I still get the old text plus the new (very messy).

I'm certain there's an obvious solution to this (presently i just don't reuse the cell, but this isn't best practice and is slow on old devices), so if someone can help I'd be very grateful.

Thanks

ED

As requested here is the method for amending the cell

static NSString *CellIdentifier = @"Cell";
TransactionCellView *cell = (TransactionCellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"TransactionCellView" owner:self options:nil];
    cell = tblCell;
}

[cell resetCell]; // clears the labels
[cell setData:[dataArray objectAtIndex:[indexPath row]]; // replaces the data and updates the labels

return cell;

}


Solved this issue!!! Amazingly simple when you know what the problem is/was.

I was using IB and "Clear Graphics Context" was not selected on my UILabels. So that is why the next text was just overlaying the old.

THanks guys for trying to help.


I suspect that you are adding the label as a subview programatically in your cellForRowAtIndexPath method. You need to make sure that you are not adding that subview every time the cell is recycled. Instead create the label only when creating a new cell (not when recycling a cell) then assign a tag value to the label and then in the future, when it gets recycled, retrieve the label by tag value and change its text.


You are correct in that the cells are being reused. Your code should be set up roughly using the following pattern:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString* identifier = @"someidentifier";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 
    if(!cell) {
        // create the cell and add all subviews to it
    } else {
        // update the cell and access appropriate subviews to modify what is displayed
    }
    return cell;
}

The cell will be created the first time the identifier is used. For all subsequent requests, the cell is pulled from the UITableView cache (via dequeueReusableCellWithIdentifier), and you can then access its subviews either by tag, index, type, or whatever mechanism you choose.

Somewhat related is that you can have multiple cell identifiers, which allows you to create multiple instances of different cells depending on the data that you have. In one of my projects, I have 4 different cells, each dependent upon the number of lines of data that they will display (anywhere between 1 and 4). This helps ensure a smooth scrolling experience regardless of how many lines the cell has since the renderer doesn't have to worry about dynamically changing the height of the cell on the fly.


Try this..

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {

        for (UIView* subView in cell.contentView.subviews)
        {       
            [subView removeFromSuperview];
        }   
       // Your Code to customize cell goes here.
 }
0

精彩评论

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