Answer the question
In order to leave comments, you need to log in
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;
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question