开发者

Table cells bleed into each other

开发者 https://www.devze.com 2023-03-25 15:13 出处:网络
I\'ve been having a strange problem with table cells that I\'ve never seen before. They are bleeding into each other when I scroll the table. For example, right now I have a cell in the first section

I've been having a strange problem with table cells that I've never seen before. They are bleeding into each other when I scroll the table. For example, right now I have a cell in the first section that says "Device" and a bunch of cells in a second section that say "Hi" and then a few blank cells in the third section. When I scroll, sometimes "Device" gets written on top of "Hi" (so both labels are visible but overlaying each other (they have clear background color) and sometimes "Hi" gets written in the third section's cells. I've been trying to figure out what's wrong but nothing seems to work.

Here are the relevant sections of my table view controller:

- (id)initWithDeviceCoordinator:(DeviceCoordinator*)dev_con
{
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
        device_coordinator = dev_con;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if (section == 0 || section == 3) return 1;
    else if (section == 1) return 8;
    else if (section == 2) return 2;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Cell开发者_开发知识库Identifier] autorelease];
    }

    if (indexPath.section == 0) cell.textLabel.text = [device_coordinator getDeviceName];
    else if (indexPath.section == 1)
    {
        if (indexPath.row == [[device_coordinator getChannelList] count])
        {
            cell.textLabel.text = @"Add new channel";
            return cell;
        }
        cell.textLabel.text = @"HI";
        return cell;
    }
    return cell;
}

All of the other functions in the class are XCode default.


You should set cell.textLabel.text = nil to start with, otherwise you will see your text in the wrong place when the cells get reused (like you said you saw Hi where it shouldn't appear). As for the overlapping labels, I don't think the code you pasted can cause that. Are you using custom cells?


I found the problem. It was with this line:

static NSString *CellIdentifier = @"Cell";

Changing it to this fixed everything:

NSString *CellIdentifier = [NSString stringWithFormat:@"Cell %d_%d",indexPath.section,indexPath.row];
0

精彩评论

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