2015-01-16 105 views
0

經常當我拉了我的UITableView刷新從我的服務器繪製的內容將與以下消息崩潰:頻繁outofbounds用的cellForRowAtIndexPath指數 - 的OBJç

Terminating app due to uncaught exception 'NSRangeException', reason: 
'-[__NSCFArray objectAtIndex:]: index (4) beyond bounds (4)' 

現在的問題是,我的數據在我的最後被設置爲每次都有相同數據的NSDictionary,所以我知道我的服務器沒有返回任何不合適的東西。

在這裏如何一切工作,我有一個整數稱爲ii將細胞再增殖的始終= 0基於:

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){ 
     //end of loading 
     //for example [activityIndicator stopAnimating]; 
     i = 0;//<!-- reset the i so that when page reloads cells it does not fail 
    } 
} 

我也在上2額外的插槽我NSDictionary因爲插槽01是關鍵部分。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [[self.dataGrabed_dictionary objectForKey:@"scheduled_times"] count] + 2;//<!-- by default we want to make sure 2 is being added no matter what. 1 = clients address and name, 2 = upcoming lawn care 
} 

所以回到i第2調用cellForRowAtIndexPath我用if statements分配所收集的數據進行第2個細胞:有與填充第2個小區的用戶信息相關聯。比我增加i我填充其他單元格的數據(if any)

這裏正好是怎麼回事內cellForRowAtIndexPath

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

    NSLog(@"%d",i); 
    self.tableView.separatorColor = [UIColor clearColor];// Get rid of speration border color 

    static NSString *CellIdentifier = @"homecell"; 

    HomePageTimelineCell *cell = (HomePageTimelineCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    cell = [[HomePageTimelineCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    [cell initTimelineCell]; 

    cell.backgroundColor = [UIColor clearColor];// Set the background color to the cell 

    // lets check if there is any data 
    if([self.dataGrabed_dictionary objectForKey:@"scheduled_times"] == nil && [self.dataGrabed_dictionary objectForKey:@"upcoming"] == nil) 
    { 
     //<!-- if the indexRow row = 0, this is the clients username and address 
     if(indexPath.row == 0) 
     { 
      NSArray *get = [[SSKeychain allAccounts] init]; 
      NSString *username = [get[0] objectForKey:@"acct"]; 
      //<!-- Get keyname of the address and than point to that keyname and get data 
      //NSString *KeyName = [[self.dataGrabed_dictionary allKeys] objectAtIndex: indexPath.row]; 
      //<!-- create uppercase strings 
      NSString *upperCaseAddress = [[self.dataGrabed_dictionary objectForKey:@"client_address"] uppercaseString]; 
      NSString *upperCaseUsername = [username uppercaseString]; 
      //<!-- Set associated strings 
      cell.addressLabel.text = upperCaseAddress; 
      cell.usernameLabel.text = upperCaseUsername; 
     } 
     else if(indexPath.row == 1) 
     { 
     cell.contentView.backgroundColor = [self colorWithHexString:@"666666"]; 
     cell.button.backgroundColor = [self colorWithHexString:@"7ddcb8"]; 
     cell.button.tag = indexPath.row; 

     [cell.button setTitle:@"schedule" forState:UIControlStateNormal]; 
     [cell.button setTitleColor:[self colorWithHexString:@"666666"] forState:UIControlStateNormal]; 
     /* Setup button action */ 
     //[cell.button addTarget:self action:@selector(onCustomAccessoryTapped:) forControlEvents:UIControlEventTouchUpInside]; 
     cell.dateLabel.tag = indexPath.row + 100; //<!-- 100 = date 
     cell.dateLabel.text = @"Nothing Scheduled";//<!-- Set the Date 
     [cell.contentView addSubview:cell.button]; 
     [cell.contentView addSubview:cell.nextLabel]; 
     [cell.contentView addSubview:cell.dayLabel]; 
     [cell.contentView addSubview:cell.dateLabel]; 
     } 
    } 
    else 
    { 

    //<!-- Set background color 
    if (indexPath.row % 2) { 
     cell.contentView.backgroundColor = [self colorWithHexString:@"77d1af"]; 
     //<!-- Set background and text color for even cells 
     [cell.button setTitleColor:[self colorWithHexString:@"7ddcb8"] forState:UIControlStateNormal]; 
     cell.button.backgroundColor = [self colorWithHexString:@"666666"]; 
    } else { 
     cell.contentView.backgroundColor = [self colorWithHexString:@"7ddcb8"]; 
     //<!-- Set background and text color for even cells 
     [cell.button setTitleColor:[self colorWithHexString:@"7ddcb8"] forState:UIControlStateNormal]; 
     cell.button.backgroundColor = [self colorWithHexString:@"ffffff"]; 
    } 
    //<!-- if the indexRow row = 0, this is the clients username and address 
    if(indexPath.row == 0) 
    { 
     NSArray *get = [[SSKeychain allAccounts] init]; 
     NSString *username = [get[0] objectForKey:@"acct"]; 
     //<!-- Get keyname of the address and than point to that keyname and get data 
     //NSString *KeyName = [[self.dataGrabed_dictionary allKeys] objectAtIndex: indexPath.row + 4]; 
     //<!-- create uppercase strings 
     NSString *upperCaseAddress = [[self.dataGrabed_dictionary objectForKey:@"client_address"] uppercaseString]; 
     NSString *upperCaseUsername = [username uppercaseString]; 
     //<!-- Set associated strings 
     cell.addressLabel.text = upperCaseAddress; 
     cell.usernameLabel.text = upperCaseUsername; 
    } 
    //<!-- if the indexRow row = 1, this is the 2 cell and will show the most upcoming lawn schedule 
    else if(indexPath.row == 1) 
    { 
     //<!-- Lets check if user even has an upcoming mowing date --> 
     if([self.dataGrabed_dictionary objectForKey:@"upcoming"] == nil) 
     { 
      cell.contentView.backgroundColor = [self colorWithHexString:@"666666"]; 
      cell.button.backgroundColor = [self colorWithHexString:@"7ddcb8"]; 
      cell.button.tag = indexPath.row; 

      [cell.button setTitle:@"schedule" forState:UIControlStateNormal]; 
      [cell.button setTitleColor:[self colorWithHexString:@"666666"] forState:UIControlStateNormal]; 
      /* Setup button action */ 
      //[cell.button addTarget:self action:@selector(onCustomAccessoryTapped:) forControlEvents:UIControlEventTouchUpInside]; 
      cell.dateLabel.tag = indexPath.row + 100; //<!-- 100 = date 
      cell.dateLabel.text = @"Nothing Scheduled";//<!-- Set the Date 
      [cell.contentView addSubview:cell.button]; 
      [cell.contentView addSubview:cell.nextLabel]; 
      [cell.contentView addSubview:cell.dayLabel]; 
      [cell.contentView addSubview:cell.dateLabel]; 
     } 
     else 
     { 
      //<!-- create uppercase strings 
      NSString *upperCaseDay = [[self.dataGrabed_dictionary objectForKey:@"upcoming_day"] uppercaseString]; 
      //<!-- create uppercase strings 
      NSString *upperCaseDate = [[self.dataGrabed_dictionary objectForKey:@"upcoming_date"] uppercaseString]; 

      cell.contentView.backgroundColor = [self colorWithHexString:@"666666"]; 
      cell.button.backgroundColor = [self colorWithHexString:@"7ddcb8"]; 
      cell.button.tag = indexPath.row; 

      [cell.button setTitle:@"change" forState:UIControlStateNormal]; 
      [cell.button setTitleColor:[self colorWithHexString:@"666666"] forState:UIControlStateNormal]; 
      /* Setup button action */ 
      //[cell.button addTarget:self action:@selector(onCustomAccessoryTapped:) forControlEvents:UIControlEventTouchUpInside]; 

      cell.dayLabel.text = upperCaseDay;//<!-- Set the day 
      cell.dateLabel.tag = indexPath.row + 100; //<!-- 100 = date 
      cell.dateLabel.text = upperCaseDate;//<!-- Set the Date 
      [cell.contentView addSubview:cell.button]; 
      [cell.contentView addSubview:cell.nextLabel]; 
      [cell.contentView addSubview:cell.dayLabel]; 
      [cell.contentView addSubview:cell.dateLabel]; 
     } 
    } 
    else //<!-- Normal cell population 
    { 
     //<!-- Re edit labels positioning 
     cell.dayLabel.frame = CGRectMake(9, 10, 200, 45); 
     cell.dateLabel.frame = CGRectMake(9, 35, 300, 45); 
     //<!-- Setup data called from server 
     NSDictionary *innerClientData =[self.dataGrabed_dictionary objectForKey:@"scheduled_times"][i]; 
     NSString *innerClientDay =[self.dataGrabed_dictionary objectForKey:@"scheduled_times_day"][i]; 
     NSString *innerClientDate =[self.dataGrabed_dictionary objectForKey:@"scheduled_times_date"][i]; 
     //<!-- create uppercase strings 
     NSString *upperCaseDay = [innerClientDay uppercaseString]; 
     //<!-- create uppercase strings 
     NSString *upperCaseDate = [ innerClientDate uppercaseString]; 

     //<!-- Check to see if client paid 
     if([[innerClientData objectForKey:@"client_paid"] isEqual: @"0"]) 
     { 
      NSString *amount = [innerClientData objectForKey:@"client_price"]; 
      NSString *pay = @"pay $"; 
      NSString * combined = [pay stringByAppendingString:amount]; 

      [cell.button setTitle:combined forState:UIControlStateNormal]; 
     } 
     else if([[innerClientData objectForKey:@"client_paid"] isEqual: @"1"]) 
     { 
      [cell.button setTitle:@"PAID" forState:UIControlStateNormal]; 
     } 
     //[cell.button setTitleColor:[self colorWithHexString:@"666666"] forState:UIControlStateNormal]; 
     cell.button.tag = indexPath.row; 
     /* Setup button action */ 
     //[cell.button addTarget:self action:@selector(onCustomAccessoryTapped:) forControlEvents:UIControlEventTouchUpInside]; 
     cell.dayLabel.text = upperCaseDay;//<!-- Set the day 
     cell.dateLabel.tag = indexPath.row + 100; //<!-- 100 = date 
     cell.dateLabel.text = upperCaseDate;//<!-- Set the Date 
     [cell.contentView addSubview:cell.button]; 
     [cell.contentView addSubview:cell.dayLabel]; 
     [cell.contentView addSubview:cell.dateLabel]; 

     i++; 

    } 
    } 
    return cell; 
} 

我的NSDictionary的分配是這樣的:

NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
NSLog(@"responseString: %@",responseString); 
      // GRAB STATUS OBJECT 
NSDictionary* json = [NSJSONSerialization 
          JSONObjectWithData:returnData //1 

          options:kNilOptions 
          error:&error]; 
self.dataGrabed_dictionary = [json objectForKey:@"retrieved_data"]; 

建議至於爲什麼它與out of bounds崩潰?

+0

它在哪裏崩潰?你沒有指出哪一行代碼崩潰。 – rmaddy

+0

爲什麼'cellForRowAtIndexPath:'如此複雜,但'numberOfRowsInSection:'是如此簡單?他們似乎不同意。 – rmaddy

+2

是'我'和實例變量?擺脫它。它應該是'indexPath.row - 2'。 – rmaddy

回答

1

最可能的問題是您獲取單元格數據的方式。您正在使用的實例變量i以下行:

NSDictionary *innerClientData =[self.dataGrabed_dictionary objectForKey:@"scheduled_times"][i]; 
    NSString *innerClientDay =[self.dataGrabed_dictionary objectForKey:@"scheduled_times_day"][i]; 
    NSString *innerClientDate =[self.dataGrabed_dictionary objectForKey:@"scheduled_times_date"][i]; 

這不會有任何效果。將i替換爲indexPath.row - 2

可以多次訪問單元並按各種順序訪問單元。