简体   繁体   中英

INSERT a new UITableView row with Editable UITextfields

I've been working on this for many days now and cannot seem to make it work. It may not even be possible but there's gotta be a way!

I want to insert a new uitableview row that contains two uitextfields that are editable after insertion. Everything I've read says you should update your data model array before the cell is added, meaning that the texfields need text in them and then added to the array BEFORE inserting the row. I tried to add a prompting text but once the cell is added, I can tap into the textfields, but I can't change what's already there (my prompting text). The textviews are unreponsive other than being able to tap into them. How to do this?

I am presenting a UIDatePicker to fill in the textfields for a single row that is created when the app first boots up. I want users to be able to add another row for when they read a book a second time (or more!). The textfields no longer update with the datePicker's values once the next row is added.

I've tried to use an alertview with textfields in it (I'm using URBAlertView) so users can add their dates before adding the new row, but I haven't been able to get it to go.

Please let me know if you need more code or if this is even possible. Thanks in advance for your help!

- (IBAction)addNewRowButtonTapped:(id)sender {

[self showDatePicker];

[self.tableView beginUpdates];

_cell.startDateTextfield.text = @"add start date";
_cell.finishDateTextfield.text = @"add finish date";

//these two lines give me errors and causes a crash
//[_book.startReadArray addObject:_cell.startDateTextfield.text];
//[_book.finishReadArray addObject:_cell.finishDateTextfield.text];

NSArray *paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[_book.startReadArray count]-1 inSection:1]];
[[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop];
_numberOfRows ++;

[self.tableView endUpdates];

}

This should be relatively simple, create a custom view with a UITextField, make sure it is properly registered with the tableView so you can dequeue it, and your UITextField should be useable.

I've setup a demo project for you that demonstrates this: https://github.com/ekscrypto/Swift-Tutorial-InsertTextfield

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM