我根本不了解如何从那里解决此问题。

这很简单,我在UITextField中添加了UITableViewCell。用户可以键入它,然后将其滚动出并返回查看后,其内容将重置为默认状态。

这与用dequeueReusableCellWithIdentifier重用旧单元格有关吗?我只是不知道如何解决它!

这是我的代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    //Stop repeating cell contents
    else for (UIView *view in cell.contentView.subviews) [view removeFromSuperview];

    //Add cell subviews here...

}

最佳答案

您不必在初始化后立即删除单元格内容,而无需重新创建它们,而是将它们重复使用,因此您的代码应如下所示

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }


}

我假设您想对单元格进行一些控制,在这种情况下,您可以尝试使用CustomCell来创建有关初始化的所有子视图。

通常,您所有的初始化都应在
if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        //ALL INITS
    }

在它外面,您应该更新添加到单元格中的值。

关于ios - UITableView内容在滚动时重置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18183485/

10-16 23:34