Let's say you have an NSMutableArray
of this data like this:
NSMutableArray *cellLabelValues = [[NSMutableArray alloc] initWithObjects: @"1",@"2",@"3",nil];
In your tableView delegates do this:
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return cellLabelValues.count;
}
You must have a button or something to receive user's intent to add new cells. Lets assume it calls this method:
-(void) addNewCell:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Enter New Value"
message:@" "
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
And write the delegate of UIAlertView
. Make sure you have made your ViewController to be UIAlertViewDelegate
:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
NSString *value = [alertView textFieldAtIndex:0].text;
[cellLabelValues addObject:value];
[tableView reloadData];
}
}
What happens here is, user taps new cell button. It takes entry from user. Then it reloads the table after adding it to your array. When it reaches numberOfRowsInSection
delegate method, it sees that the array is now incremented by 1. So it will show one extra cell.