Dan
Dan

Reputation: 2344

iOS 6 table scrolling causing issues

There are two sections in my table view, one for test status and one for results.

The third row in the first section gets changed upon the test completing. During the test it documents the percentage complete and after it changes to showing if any problems have been detected.

The color of the text also changes to indicate bad/ok/good. For this particular test there are about 20 rows of results.

The problem being that within those 20 results two cells are being treated like the third cell in section one and turning red.

Now I assume as I scroll the table reloads which must mean my code is incorrect and has only be shown up by a test with lots of results. Any help would be great. I suspect it is the code below:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    UITableViewCell *serverLoc = [tableView dequeueReusableCellWithIdentifier:@"speedCell"];
    //   UITableViewCell *switchCell = [tableView dequeueReusableCellWithIdentifier:@"switchCell"];

    switch (indexPath.section) {
        case 0:
            switch (indexPath.row) {
                case 0:
                    serverLoc.textLabel.text = @"Test location:";
                    serverLoc.detailTextLabel.text = testLocation;
                    serverLoc.selectionStyle = UITableViewCellSelectionStyleNone;
                    serverLoc.userInteractionEnabled = NO;
                    break;
                case 1:
                    serverLoc.textLabel.text = @"Status:";
                    serverLoc.detailTextLabel.text = statusText;
                    serverLoc.selectionStyle = UITableViewCellSelectionStyleNone;
                    serverLoc.userInteractionEnabled = NO;
                    break;
                case 2:
                    if ([TestEnded isEqualToString:@"no"]) {
                        serverLoc.textLabel.text = @"Progress";
                        serverLoc.selectionStyle = UITableViewCellSelectionStyleNone;
                        serverLoc.userInteractionEnabled = NO;
                        serverLoc.detailTextLabel.text = [NSString stringWithFormat:@"%ld%%", (long)progressInt];
                        break;
                    }
                    else {

                        serverLoc.selectionStyle = UITableViewCellSelectionStyleBlue;
                        serverLoc.userInteractionEnabled = YES;
                        serverLoc.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                        serverLoc.textLabel.text = @"Problems Detected:";
                        serverLoc.detailTextLabel.text = [NSString stringWithFormat:@"%ld", (long)problemsDetected];

                        if (problemsDetected == 0) {
                            serverLoc.textLabel.textColor = [UIColor colorWithRed:0.0 / 255 green:102.0 / 255 blue:51.0 / 255 alpha:1.0];
                            serverLoc.detailTextLabel.textColor = [UIColor colorWithRed:0.0 / 255 green:102.0 / 255 blue:51.0 / 255 alpha:1.0];
                        }
                        else if (problemsDetected == 1) {
                            serverLoc.textLabel.textColor = [UIColor colorWithRed:226.0 / 255 green:232.0 / 255 blue:52.0 / 255 alpha:1.0];
                            serverLoc.detailTextLabel.textColor = [UIColor colorWithRed:226.0 / 255 green:232.0 / 255 blue:52.0 / 255 alpha:1.0];
                        }
                        else {
                            serverLoc.textLabel.textColor = [UIColor redColor];
                            serverLoc.detailTextLabel.textColor = [UIColor redColor];
                        }

                    }
                    break;    
            }
            break;

I wasn't sure if it would work when I first implemented it. It "did" but clearly I wasn't needing to scroll enough to unveil the bug.

Thanks in advance for any pointers

Upvotes: 0

Views: 417

Answers (1)

Bryce Buchanan
Bryce Buchanan

Reputation: 279

The code here looks fine... It sounds like your table is reusing the cell set up for this particular path but not returning the values to default before reusing the cell. (e.g. in the section == 1 case: severLoc.textLabel.textColor = [UIColor blackColor];) This will also show up if case 0,0 and 0,1 end up with a 0,2 cell when grabbing a reused cell.

Upvotes: 1

Related Questions