V
V
Vitaly2016-12-20 14:00:32
Objective-C
Vitaly, 2016-12-20 14:00:32

Why, when scrolling the table, the data in the cell-e, which goes out of visibility, is null when returning?

All the best, I ran into such a problem and I don’t know what the reason is :(
Here is the code where I create cells:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return [_animalsArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    static NSString* Cellid = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cellid];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:Cellid];
        NSLog(@"nill cell");
    }

    

    MyAnimals* animal = [_animalsArray objectAtIndex:indexPath.row];
    NSLog(@"### name - %@ %@", animal.name, animal.type);

    
    cell.textLabel.text = [[_animalsArray objectAtIndex:indexPath.row] name];
     cell.textLabel.text = [NSString stringWithFormat:@"%@",animal.name];
    UIImage* animalImg = [[_animalsArray objectAtIndex:indexPath.row] photo] != NULL ? [UIImage imageWithData:[[_animalsArray objectAtIndex:indexPath.row] photo]] : [UIImage imageNamed:@"camera.png"];
    cell.imageView.image = animalImg;
    cell.detailTextLabel.text =  [NSString stringWithFormat:@"type: %@; ownColor: %@; age: %d" ,animal.type, animal.ownColor, animal.age];

    return cell;
}

As a result, after a strong scroll, I get this:
99a2099171d74e4481661ece086b9f8b.png

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
ManWithBear, 2016-12-20
@ManWithBear

Everything is fine with the cells, see what you do with the array and what you have in the logs.
By code:
Double empty lines are not allowed.
You create the myAnimal variable, but then you continue to get the object from the array.

MyAnimals* animal = ... <- bad
MyAnimal *animal = ... <- good

[_animalsArray objectAtIndex:indexPath.row] <- bad
self.animalsArray[indexPath.row] <- good

[[_animalsArray objectAtIndex:indexPath.row] photo] != NULL ? ... : ... <- bad
self.animalsArray[indexPath.row].photo != nil ? ... : .... <- good
self.animalsArray[indexPath.row].photo ? ... : .... <- better

Избыточно:
cell.textLabel.text = [[_animalsArray objectAtIndex:indexPath.row] name];
cell.textLabel.text = [NSString stringWithFormat:@"%@",animal.name];

Достаточно:
cell.textLabel.text = animal.name;

Где-то у вас:
@property (nonatomic) NSArray *animalsArray; <- bad
@property (nonatomic) NSArray *animals; <- good
@property (nonatomic) NSArray<MyAnimal *> *animals; <- better

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question