Answer the question
In order to leave comments, you need to log in
How to update UITableView data in objective c?
All the best. Here I play with tables.
I created a table
.h
@property (strong,nonatomic) NSMutableArray* animals;
@property (weak, nonatomic) IBOutlet UITableView *animalsTable;
.m
- (void)viewDidLoad {
self.animalsTable.dataSource = self;
self.animalsTable.delegate = self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_animals 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];
}
UIImage* simpleAnimals = [UIImage imageNamed:@"Tiere-coloured.png"];
cell.imageView.image = simpleAnimals;
cell.textLabel.text = [[_animals objectAtIndex: indexPath.row] name];
NSString* deatail = [NSString stringWithFormat:@"age: %@; color: %@" ,[[_animals objectAtIndex: indexPath.row] age],[[_animals objectAtIndex: indexPath.row] ownColor]];
cell.detailTextLabel.text = deatail;
return cell;
}
Answer the question
In order to leave comments, you need to log in
dequeueReusableCellWithIdentifier - better use another method - dequeueReusableCellWithIdentifier:indexPath and then the if (cell == Nil) check will never be called, the cell is always there and the code can be shortened accordingly. It's better to use your own class for the cell, a subclass of UITableViewCell, it's easier to set it up, then just call reloadData and the table is reloaded. also set the table type to storyboard - dynamic
When adding data to a table, use the beginUpdates and endUpdates methods. Adding, as well as removing in this case, is carried out with animation.
It is desirable that insertSection correspond to the index of the object in the array. If an object is added to the beginning of the array, then the insertSection index must be zero.
[self.tableView beginUpdates];
NSIndexSet* insertSection = [NSIndexSet indexSetWithIndex:0];
[self.tableView insertSections:insertSection withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question