0

This is my code ,I am trying to add value in Array and populate them into Tableview .

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.greekLetter count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *SimpleIdentifier = @"SimpleIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleIdentifier];

    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleIdentifier];
    }

    cell.textLabel.text = self.greekLetter[indexPath.row];

    return cell;
}

when i add the value herd code value it is showing correct in table view.

self.greekLetter=@[@"prem",@"nath",@"kumar",@"shree"];

I want to add value dynamically to the array and it will show in table view. Can some one pls help me.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
prem nath
  • 75
  • 1
  • 12

2 Answers2

4

Instead of reloading the entire UITableView you could try just using insertRowsAtIndexPaths. It doesn't reload all the cells and gives you a nice animation on top of it.

For example:

-(void)addData:(NSString *)text{

    //provided your self.greekLetter is NSMutableArray 

    [self.greekLetter addObject:text]

    NSIndexPath *index = [NSIndexPath indexPathForRow:(self.greekLetter.count - 1) inSection:0];

    [self.tableview insertRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationRight];
}
Rikh
  • 4,078
  • 3
  • 15
  • 35
2

Assuming that your greekLetter is NSMutableArray:

-(void) addDataToGreekLetter {
    [self.greekLetter addObject:@"TestObject"];
    [self.tableView reloadData];
}

Assuming that your greekLetter is NSArray:

-(void) addDataToGreekLetter {
    NSMutableArray *temp = [NSMutableArray arrayWithArray: self.greekLetter];
    [temp addObject:@"TestObject"];
    self.greekLetter = [NSArray arrayWithArray:temp];
    [self.tableView reloadData];
}
Miknash
  • 7,888
  • 3
  • 34
  • 46