V
V
Vitaly2016-11-29 19:04:26
Objective-C
Vitaly, 2016-11-29 19:04:26

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

with data from NSMutableArray* animals , then I change this array by adding new data to it, but after that I can’t update the table data in any way :( Of course, you can recreate this table after adding an element to the array, but I feel that this is not possible.
option [_animalsTable reloadData] doesn't work :(
Please tell me

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
I
i_allar, 2016-12-03
@i_allar

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

A
AndreySlimus, 2016-11-29
@AndreySlimus

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];

Above is the code to insert the section. To insert a cell into a section, the following method is suitable:
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question