简体   繁体   English

如何使用用户修改的内容动态增长UITableView行

[英]How to make a UITableView row grow dynamically with the user modified content

I am asking this question already knowing the answer is "no you can't" but in the hopes someone has a brilliant idea here we go. 我问这个问题已经知道答案是“不,你不能”,但希望有人在这里有一个好主意。

I have a subclass of UITableViewCell that has a few different subviews one of witch is a UITextField that I have as user editable. 我有一个UITableViewCell的子类,它有一些不同的子视图,其中一个是我用户可编辑的UITextField。 So naturally the textField grows with the text that is entered. 因此,textField自然会随输入的文本一起增长。

Now the question is how could I get the tableview row to grow with the textField. 现在的问题是如何通过textField使tableview行增长。

I have a variety of different size cells so I know how to use - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath but the problem is that I need to modify the after the cell is already in the tableview. 我有各种不同大小的单元格,所以我知道如何使用- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath但问题是我需要修改单元格已经在tableview中。

Also note that the previously noted delegate method gets called before the data source delegate method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 还要注意前面提到的委托方法在数据源委托方法之前被调用- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

I have thought of just pulling up another view for the user to edit then putting that data into my data model then I would have to force the tableview to reload. 我想到只是拉出另一个视图供用户编辑然后将数据放入我的数据模型然后我将不得不强制tableview重新加载。 (Can I reload just a certain cell/row) (我可以重新加载某个单元格/行)

Interestingly enough I think apple is doing what I want in their iTunes U app. 有趣的是,我认为苹果正在他们的iTunes U应用程序中做我想做的事情。 When you tap a assignment it expands. 当您点按作业时,它会展开。 I think they are using tableviews their right? 我认为他们正在使用tableviews吗?

I know I have a lot of questions and talk here but I believe it is all related and just to show what I have researched. 我知道我有很多问题并在这里讨论,但我相信这一切都是相关的,只是为了展示我所研究的内容。 I am just looking for the possibility that one of you has a stroke of genius. 我只是在寻找你们其中一个人有天才的可能性。

Hopefully this can help others also because this seems to be a hot topic but no one ask the question well or gets good answers. 希望这也可以帮助其他人,因为这似乎是一个热门话题,但没有人问好问题或得到好的答案。

Actually, this has been asked and answered many times -- it's not impossible. 实际上,这已被多次询问和回答 - 这并非不可能。 Search for "UITableView custom row height" or "UITableView multi line UITextField" or similar and you'll find several well-answered questions. 搜索“UITableView自定义行高”或“UITableView多行UITextField”或类似内容,您将找到几个回答良好的问题。

You're on the right track -- you need to return a height in tableView:heightForRowAtIndexPath: , even though that method is called before you create/configure the cell. 你在正确的轨道上 - 你需要在tableView:heightForRowAtIndexPath:返回一个高度,即使在创建/配置单元格之前调用了该方法。 This is okay... you just need a way to compute that height without having the cell. 这没关系...你只需要一种方法来计算那个高度而不需要单元格。 Other answers reference -[NSString sizeWithFont:constrainedToSize:] and related methods... this should get you on the right track. 其他答案参考-[NSString sizeWithFont:constrainedToSize:]和相关方法......这应该让你走在正确的轨道上。

Changing the height while the text field is editing is less obvious, but this answer has the key... if you call 在文本字段编辑时更改高度不太明显,但这个答案有关键...如果你打电话

[tableView beginUpdates];
[tableView endUpdates];

the table view will not only ask its delegate for heightForRowAtIndexPath: again and resize the cell to match, it'll do it with a smooth animation. 表视图不仅会再次询问其委托的heightForRowAtIndexPath:并调整单元格以匹配,它将以平滑的动画来完成。

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

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