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.

Related posts

Recent Viewed