UITableview Scroll 擦除 UITableviewcell 内文本字段中的数据 [英] UITableview Scroll erases data in text field inside UITableviewcell

查看:12
本文介绍了UITableview Scroll 擦除 UITableviewcell 内文本字段中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 tableview 单元格中有一个带有 UITextfield 的 UITableViewController.如果我滚动表格视图,用户在文本字段中输入的数据就会消失.我试图将文本字段数据添加到 NSMutableArray 但它仍然没有工作.请提供任何帮助.

解决方案

cellForRowAtIndexPath: 被调用时,你返回的单元格必须完全填充你想要显示的任何数据.因此,如果单元格包含 UITextfield,您需要将它的 text 属性设置为数据中该行的正确值.

当表格单元格从屏幕的顶部或底部消失时,UITableViewCell 本身可以重新使用.(滚动时,单元格消失,新单元格出现,但 UITableView 类正在重新使用 UITableViewCell 对象.)在 cellForRowAtIndexPath: 中,当您使用缓存的单元格时,您必须确保设置您希望它为相关行显示的所有内容,否则您可能会在表格中看到一些奇怪的行为.

这有帮助吗?

以下是 cellForRowAtIndexPath: 中使用的典型模式的示例.注意 dequeueReusableCellWithIdentifier: 的使用.该方法返回一个先前分配但未使用的 UITableViewCell,如果有的话.进一步注意,如果没有返回缓存的单元格,代码会创建一个新的单元格,并对其进行设置(使用与任何可能特定于行的内容无关的内容).之后,您可以根据需要为相关行设置单元格.

<前>- (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath{静态 NSString *SearchResultsCellIdentifier = @"SearchResultsCellIdentifier";UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:SearchResultsCellIdentifier];如果(单元格 == 零){cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle重用标识符:SearchResultsCellIdentifier] 自动释放];cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;}//此处单元格的行特定设置//...返回单元格;}

查看文档以了解有关这些方法的详细信息.Apple 和其他地方有很多关于如何实现 tableView 的示例.

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.

解决方案

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.

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.

Does this help?

EDIT:

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;
}

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆