1

我有一个由对象数组填充的表,每当我调用重新填充数组并重新加载表的刷新方法时,当我尝试在 cellForRowAtIndexPath 中读取我的数组时,都会出现超出范围的错误。我已经对我的数组进行了 NSLogged 以确保它被正确填充,并且确实如此。它总是试图读取比数组实际更大的索引 1。我不知道为什么。任何帮助将不胜感激!

cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSLog(@"IndexPath = %d", indexPath.row);

// Returns the cell for the given indexPath
static NSString *cellidentifier1 = @"cell1";
static NSString *cellidentifier2 = @"cell2";

self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"dark-background.jpg"]];
self.tableView.separatorColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"dark-background.jpg"]];

// Invisible Cell
if (indexPath.row % 2 == 0) {

    UITableViewCell *cell2 = [theTableView dequeueReusableCellWithIdentifier:cellidentifier2];

    if (cell2 == nil) {

        cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier2];
    }

    [cell2.contentView setAlpha:0];
    [cell2 setUserInteractionEnabled:NO];
    [cell2 setBackgroundColor:[UIColor clearColor]];

    return cell2;
}

// Standard Cell                    
FCTableViewCell *cell = (FCTableViewCell *)[theTableView dequeueReusableCellWithIdentifier:cellidentifier1];

if (cell == nil) {

    cell = [[FCTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier1];
}

FCCall *currentCall = [[dataController calls] objectAtIndex:((indexPath.row - 1 ) / 2)];

cell.callLabel.text = currentCall.call;
cell.locationLabel.text = currentCall.location;
cell.wcccaNumberLabel.text = currentCall.wcccaNumber;
cell.callNumberLabel.text = currentCall.callnumber;

// Remove leading white space from units string
NSString *units = [currentCall.units stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

cell.unitsLabel.text = units;
cell.stationLabel.text = currentCall.station;

if ([currentCall.county isEqualToString:@"W"]) {

    cell.countyImageView.image = [UIImage imageNamed:@"blue.png"];
}
else {
    cell.countyImageView.image = [UIImage imageNamed:@"green.png"];
}

if ([currentCall.callType isEqualToString:@"F"]) {

    cell.callTypeImageView.image = [UIImage imageNamed:@"red.png"];
}
else {
    cell.callTypeImageView.image = [UIImage imageNamed:@"yellow.png"];
}

[cell.unitButton addTarget:self action:@selector(unitButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
cell.unitButton.tag = ((indexPath.section & 0xFFFF) << 16) |
(indexPath.row & 0xFFFF);

[cell.directionsButton addTarget:self action:@selector(directionsButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
cell.directionsButton.tag = ((indexPath.section & 0xFFFF) << 16) |
(indexPath.row & 0xFFFF);

return cell;
}

numberOfRowsInSection:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // Returns the number of rows per section        
    return [dataController calls].count * 2;
}

刷新方法:

[dataController refreshData:self success:^(NSURLRequest *request, NSURL *url, NSArray *calls) {

        [self setWork:calls];
        NSLog(@"Array = %@", work);

        // Reset the pull controller and reload the table
        [pull finishedLoading];
        [tableView reloadData];

    } failure:^(NSURLRequest *request, NSURL *url, NSError *error) {

        // Reset pull indicator
        [pull finishedLoading];

        // Reload table
        [tableView reloadData];
    }];

刷新方法:

- (void)refreshData:(id)object success:(void (^)(NSURLRequest *request, NSURL *url, NSArray *calls))success failure:(void (^)(NSURLRequest *request, NSURL *url, NSError *error))failure
{
    NSLog(@"Refresh Started");

    // Start the network activity indicator
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

    // Check to make sure we can even make an HTTP request
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.wccca.com/PITS"]];
    AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSLog(@"Reachable");

        // Get the URL we are going to use to parse with
        [FCDataController parserURL:[NSURL URLWithString:@"http://www.wccca.com/PITS"] completion:^(NSURL *url) {

            NSURLRequest *parserRequest = [NSURLRequest requestWithURL:url];
            AFXMLRequestOperation *operation = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:parserRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) {

                // Initialize our calls array that will control our calls
                calls = [[NSMutableArray alloc] init];

                // Set the delegate for the XMLParser and start the parse operation
                XMLParser.delegate = self;
                BOOL successful = [XMLParser parse];

                // Determine if the parse operation was a success or not
                if (!successful) {

                    // Return the failure block because the parser failed
                    failure(request, url, [FCErrors parserError]);

                    // Stop the network activity indicator
                    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
                }
                else {

                    // Return the success block
                    success(request, url, calls);

                    // Stop the network activity indicator
                    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
                }

                NSLog(@"Refresh Finished");

            } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLParser *XMLParser) {

                NSLog(@"AFXMLOperation Error: %@", error.localizedDescription);

                // Remove all data from our previous calls aray
                [self setCalls:nil];

                failure(parserRequest, url, [FCErrors badURLError]);

                // Stop the network activity indicator
                [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

                NSLog(@"Refresh Finished");
            }];
            [operation start];
        }];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"Unreachable. AFHTTPRequestOperation Error: %@", error.localizedDescription);

        // Remove all data from our previous calls aray
        [self setCalls:nil];

        failure(request, nil, [FCErrors networkError]);

        // Stop the network activity indicator
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

        NSLog(@"Refresh Finished");
    }];
    [requestOperation start];
}
4

0 回答 0