indexPath does not match the content in tableView

Asked

Viewed 81 times

0

I’m carrying a tableView from a array laden with arrays, this whole charging process is working perfectly, however, when I update the tableView (reloadData) with the same information and I have the scroll in the middle of the list, for example, the index’s get lost, that is, when I access one cell, it loads information from another. On the other hand, if the Scroll is at the top of the tableView, even updating, this problem does not occur. Follow code:

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

    static NSString *CellIdentifier = @"cell_aux";

    clsTableViewCell *cell = (clsTableViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"cell_aux" owner:self options:nil];
        cell = (clsTableViewCell *)[nib objectAtIndex:0];
    }

    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

    arInfos = [[NSArray alloc] initWithArray:[[arMain objectAtIndex:indexPath.row]                  componentsSeparatedByString:@"|"]];

    [arInfosAll addObject:arInfos];

    NSURL *imgURL = [NSURL URLWithString:[arInfos objectAtIndex:4]];

    UIImage *Noimg=[UIImage imageNamed:@"NoImage.png"];
    cell.img.image = Noimg;


    cell.lblNome.text = [arInfos objectAtIndex:1];
    cell.lblEndereco.text =[arInfos objectAtIndex:2];

    NSURLRequest* request = [NSURLRequest requestWithURL:imgURL];
    [NSURLConnection sendAsynchronousRequest:request
                                           queue:[NSOperationQueue mainQueue]
                               completionHandler:^(NSURLResponse * response,
                                                   NSData *imgData,
                                                   NSError * error)
        {
                                   if (!error)
                                   {
                                       UIImage *img = [UIImage imageWithData:imgData];
                                       cell.img.image = img;
                                   }

                               }];
        }

    return cell;
}
  • Why are you doing this [arInfosAll addObject:arInfos];? One more question, why don’t you use Nsdictionary for each input of your array instead of another array?

  • Actually I could use Nsdictionary actually, but I ended up clicking on an array, because the content that fills it, was a string delimited by "|" Pipes, so I used arInfos = [[[Nsarray alloc] initWithArray:[[arrayAux objectAtIndex:indexPath.Row] componentsSeparatedByString:@"|"]]; The Usage [arInfosAll addObject:arInfos] is to store each array, as I will use it further. But it can influence this problem I’m facing?

  • One of your problems eh q arInfos eh uma Ivar. try to define and allocate it within the cellForRowAtIndexPath. ou seja, troque o arInfos = [[NSArray alloc] initWithArray:[[arMain objectAtIndex:indexPath.row] por NSArray *arInfos = [[NSArray alloc] initWithArray:[[arMain objectAtIndex:indexPath.row]

  • Hello, Thank you so much for the return! I made the recommended changes, but if I think there must be some other problem, as it has not yet worked, unfortunately. I also ran a test without "sendAsynchronousRequest" and the problem persisted.

1 answer

1

I believe the problem you’re having is because of:
arInfos = [[Nsarray alloc] initWithArray:[[arMain objectAtIndex:indexPath.Row]

You point the arInfos to an array and then add it to arInfosAll The problem eh q in the other interaction the arInfos starts pointing to another array, so it turns out that arInfosAll is pointed to several equal arrays (the last one to be assigned to Arinfos)

change

arInfos = [[NSArray alloc] initWithArray:[[arMain objectAtIndex:indexPath.row]  

for

NSArray *arInfos = [[NSArray alloc] initWithArray:[[arMain objectAtIndex:indexPath.row]
  • Hello, Thank you so much for the return! I made the recommended changes, but if I think there must be some other problem, as it has not yet worked, unfortunately. I also ran a test without "sendAsynchronousRequest" and the problem persisted.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.