1

オブジェクトの配列によって移入されたテーブルがあり、配列を再移入してテーブルをリロードする refresh メソッドを呼び出すたびに、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