Hello. I have a navigation based application, and the UITableView is populated with custom cells I've created in Interface Builder. Each cell has a UILabel and a UITextField in it. When the tableView is in editing mode, you can edit the textfield. When it isn't, you can't edit it, and when the user taps on it the cell, it goes to a separate view controller. When the user clicks the add cell button at the top, it adds a new cell. However, when I do this, in order for the new cell to show up, I have to call [tableView reloadData]. Which erases what the user inputted into the textfield. I want to be able to reload the data without it doing this. Here is some of the code:
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
for (id currentObject in topLevelObjects) {
if([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (CustomCell *) currentObject;
break;
}
}
}
return cell;
}
- (void)addRow:(id)sender; {
if(numberOfViewsAndRows < 10) {
[self setEditing:YES animated:YES];
CustomCell *theCell = (CustomCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
// allocate a view and add to the view array as a dictionary item
theCell.textField.text = [NSString stringWithFormat:@"Untitled"];
TheViewController *theViewController = [[TheViewController alloc] init];
ptheViewController.title = [NSString stringWithFormat:@"%@", theCell.textField.text];
[views addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"Untitled", @"title", theViewController, @"controller", nil]];
[theViewController release];
[self.tableView reloadData];
numberOfViewsAndRows += 1;
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Too Many Cells" message:@"You can only have up to 10." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
Here is a video showing what I'm trying to explain:
YouTube - UITableView Problem