Bernard
Bernard

Reputation: 4580

How to update UITableView after adding a row?

I have a very simple UITableView. I initialize it here with creating some object and adding them to the tableview which works perfect:

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];


    //Instantiate
    lapTimerModel = [[LTModel alloc] init];
    [lapTimerModel addChallenge:@"I made it"];
    [lapTimerModel addChallenge:@"Say the alphabet"];
    [lapTimerModel addChallenge:@"100 Meter Sprint Challenge"];
    [lapTimerModel addChallenge:@"Read the csci342 al spec challenge"];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [lapTimerModel numberOfChallenges];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if(cell == nil){

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    LTChallenge *challenge = [lapTimerModel challengeAtIndex:indexPath.row];
    cell.textLabel.text = challenge.name;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

Now here I define a button and I want to add another row to the table, I have created a new object and added to the list of other objects but it is not added to the TableView? I assume I must call something to go again throw list of my objects and bring new ones as well.

- (IBAction)newAlert:(id)sender {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Enter Name"
                                                    message:@"  "
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"OK", nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alert show];
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

    if (buttonIndex == 1) {
        NSString *name = [alertView textFieldAtIndex:0].text;
        NSLog(@"%@",name);
        //Create a new challange
        [lapTimerModel addChallenge:name];
        //[self.tableView reloadData]; Not working?!

    }
}


@end

I can see what I have typed in the alert in the console.

Upvotes: 0

Views: 229

Answers (2)

Rool Paap
Rool Paap

Reputation: 1976

You should create an iVar for your lapTimerModel

@property (strong, nonatomic) LTModel *lapTimerModel;

and call the self.lapTimerModel in all the UITableViewDelegate functions.

Instead of calling the reload method, you could call

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

    if (buttonIndex == 1) {
        NSString *name = [alertView textFieldAtIndex:0].text;
        NSLog(@"%@",name);
        //Create a new challange
        [self.lapTimerModel addChallenge:name];
        //[self.tableView reloadData]; Not working?!
        [[self tableView] beginUpdates];
        [[self tableView] reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
        [[self tableView] endUpdates];
    }
}

Upvotes: 0

Rashad
Rashad

Reputation: 11197

Check these:

  1. You've set tableView delegate and dataSource properly.
  2. Your newly added data is actually adding to dataSource.
  3. if (buttonIndex == 1) is true & [self.tableView reloadData] is getting called.

Hope this helps.. :)

Upvotes: 1

Related Questions