UITableview Scroll擦除UITableviewcell内的文本字段中的数据 [英] UITableview Scroll erases data in text field inside UITableviewcell
问题描述
我在tableview单元格中有一个带UITextfield的UITableViewController。如果我滚动表格视图,用户在文本字段中输入的数据将消失。我试图将文本字段数据添加到NSMutableArray但它仍然无法正常工作。请帮忙。
I have a UITableViewController with UITextfield inside the tableview cells. If I scroll the table view, the user entered data in the textfields disappears. I tried to add the textfield data to a NSMutableArray but it still didn't work. Any help please.
推荐答案
当调用 cellForRowAtIndexPath:
时,单元格你必须完全填写你想要显示的数据。因此,如果单元格包含UITextfield,则需要将其 text
属性设置为数据中该行的正确值。
When cellForRowAtIndexPath:
is called, the cell you return has to be completely filled in with whatever data you want to show. So, if the cell includes a UITextfield, you'll need to set it's text
property to the right value for that row in your data.
当表格单元格从屏幕的顶部或底部消失时,UITableViewCell本身可以重复使用。 (滚动时,单元格会消失,新单元格会出现,但UITableView类会重新使用UITableViewCell对象。)在 cellForRowAtIndexPath:
中获取要使用的缓存单元格,你必须确保设置你想要显示的所有行的所有内容,否则你可能会在表中看到一些奇怪的行为。
When a table cell disappears off the top or bottom of the screen, the UITableViewCell itself becomes available for re-use. (As you scroll, cells disappear, and new cells appear, but the UITableView class is re-using the UITableViewCell objects.) In cellForRowAtIndexPath:
when you get a cached cell to use, you have to be sure to setup everything you want it to show for the row in question, otherwise you might see some odd behavior in your table.
这有帮助吗?
编辑:
以下是 cellForRowAtIndexPath中使用的典型模式的示例:
。注意使用 dequeueReusableCellWithIdentifier:
。该方法返回先前已分配但未使用的UITableViewCell(如果有)。进一步注意,如果没有返回缓存的单元格,代码会创建一个新的单元格并进行设置(使用与可能是行特定的任何内容无关的内容)。接下来,您可以根据需要为相关行设置单元格。
Here's an example of the typical pattern used in cellForRowAtIndexPath:
. Notice the use of dequeueReusableCellWithIdentifier:
. That method returns a previously allocated but not in use UITableViewCell, if there is one. Notice further that if no cached cell is returned, the code creates a new one, and sets it up (with stuff that is independent of anything that might be row specific). Following that, you'd setup the cell as you need it for the row in question.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SearchResultsCellIdentifier = @"SearchResultsCellIdentifer";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:SearchResultsCellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:SearchResultsCellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// Your row-specific setup of the cell here
// ...
return cell;
}
检查文档以了解有关这些方法的详细信息。 Apple和其他地方有很多关于如何实现tableViews的例子。
Check the docs for specifics about these methods. There are LOTS of examples from Apple and elsewhere about how to implement tableViews.
这篇关于UITableview Scroll擦除UITableviewcell内的文本字段中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!