@Scorpiored88

Почему при скроле таблицы данные в cell-e, который уходит из видимости, при возвращениий равен null?

Всем доброго вот столкнулся с такой проблемой и не знаю в чем причина :(

Вот код где создаю cell-ы :

- (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;
}


В итоге, после сильного скрола получаю вот такое:

99a2099171d74e4481661ece086b9f8b.png
  • Вопрос задан
  • 137 просмотров
Пригласить эксперта
Ответы на вопрос 1
ManWithBear
@ManWithBear
Swift Adept, Prague
Всё нормально с клетками, смотрите что вы делаете с массивом и что у вас в логах.

По коду:
Двойные пустые линии недопустимы.
Вы создаете переменную myAnimal, но дальше продолжаете доставать объект из массива.
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
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы