iOS 6 table scrolling causing issues

懵懂的女人 提交于 2019-12-13 03:06:40

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/15170260/ios-6-table-scrolling-causing-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!