简体   繁体   English

用可编辑的UITextfields插入新的UITableView行

[英]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. 我想插入一个新的uitableview行,其中包含两个在插入后可编辑的uitextfields。 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. 我读过的所有内容都表明您应该在添加单元之前更新数据模型数组,这意味着texfields需要在其中添加文本,然后在插入行之前将其添加到数组中。 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. 我正在提供一个UIDatePicker来填充应用程序首次启动时创建的单行的文本字段。 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. 添加下一行后,文本字段将不再使用datePicker的值进行更新。

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. 我试图在其中使用带有文本字段的Alertview(我使用的是URBAlertView),以便用户可以在添加新行之前添加日期,但是我无法将其保留。

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. 这应该相对简单,使用UITextField创建一个自定义视图,确保已将其正确注册到tableView中,以便可以将其出队,并且UITextField应该可用。

I've setup a demo project for you that demonstrates this: https://github.com/ekscrypto/Swift-Tutorial-InsertTextfield 我为您设置了一个演示项目,演示了这一点: https : //github.com/ekscrypto/Swift-Tutorial-InsertTextfield

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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