I have been struggling with this thing for a while so I finally decided to ask to see what I’m doing wrong.
I created a custom UITableViewCell that has a UITextField as a subView in the cells Content View. What I’m trying to accomplish is to insert a new row below it with the standard placeholder when the textView begins editing. I assumed this would be possible in the method below but I always get a crash. Is there a way to do this?
I’m new to iOS and tableviews still get me everytime.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
UITableView *tableView = (UITableView*)self.view;
if ([textField tag] == 2) { // Only add row if textfield 2 is editing
[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[tableView indexPathForSelectedRow]] withRowAnimation:UITableViewRowAnimationTop];
[tableView endUpdates];
}
[textField setFont:[UIFont systemFontOfSize:18.0]];
[textField setTextColor:[UIColor blueColor]];
}
If someone has some pointers or some reference (Aside from apple’s documentation) it will be appreciated.
You are not updating the collection which is the source for the
tableViewbefore insertion.What this does is it immediately queries the data source and delegate. Now you may want to insert the “place holder”, but does your array or dictionary hold whatever is required to return the data correctly? If you are going to insert a new row and the source is not updated correctly, it will crash. So to rectify it, you need to modify the source before you begin the updates for the
tableView.I know you don’t want apple’s doc, but have a look at this and it might solve your problem.
http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html#//apple_ref/doc/uid/TP40007451-CH10-SW9