代码之家  ›  专栏  ›  技术社区  ›  Wolfgang Schreurs

UITableViewCells在屏幕上不可见时重新排序

  •  2
  • Wolfgang Schreurs  · 技术社区  · 15 年前

    我有一个问题,很难找到解决方案,即使我认为很多人应该有类似的问题。希望有人能帮助我。

    我已经使用UITableView为我的应用程序创建了一个设置屏幕。NSArray用于配置我要显示的控件。该数组如下所示:

    [self setControlArray:[NSArray arrayWithObjects:
                     [NSArray arrayWithObjects:
                      [NSDictionary dictionaryWithObjectsAndKeys:@"Username", @"text", nil], 
                      [NSDictionary dictionaryWithObjectsAndKeys:@"Password", @"text", nil], 
                      [NSDictionary dictionaryWithObjectsAndKeys:@"Server", @"text", nil], 
                      [NSDictionary dictionaryWithObjectsAndKeys:@"Port", @"text", nil], 
                      [NSDictionary dictionaryWithObjectsAndKeys:@"Use SSL", @"text", nil], 
                      nil],
                     nil]];
    

    对于数组中的每个项,我都以以下方式(在ViewDidLoad中)创建一个控件:

    // TextField: USERNAME
    username = [[UITextField alloc] initWithFrame:CGRectMake(99.0, 15.0, 190.0, 18.0)];
    [self layoutTextField:username withFont:[UIFont systemFontOfSize:13.0] keyboardType:UIKeyboardTypeAlphabet placeholder:@"Username" clearsOnBeginEditing:NO];
    [username setText:[Settings sipUser]];          
    

    最后,使用[tableView:CellForRowatineXpath]消息将每个控件添加到UITableView:

    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    
        NSString *label = [[[controlArray objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]] objectForKey:@"text"];
        [[cell textLabel] setText:label];
    
        // TextField: USERNAME
        if ([[[[controlArray objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]] objectForKey:@"text"] isEqual:@"Username"]) {
            [[cell contentView] addSubview:username];
    }
    return cell;
    

    每当我的数组中的一个控件因为滚动而失去焦点(从屏幕上消失),看不见的控件就会重新排序。我会假设控件被释放并在视线中滚动时重新创建,但是无论我尝试什么,我都找不到解决这个问题的方法。

    2 回复  |  直到 15 年前
        1
  •  2
  •   Adam Woś    15 年前

    你的问题是,你正在使用 [tableView dequeueReusableCellWithIdentifier:CellIdentifier] 这实际上可以返回一个非零单元格(例如,使用其他按钮之一),然后您只需返回它,使其看起来是(几乎)随机地重新排列。

    你必须做出决定 else 在代码中进行分支,如果出列的可重用单元不是nil:

    • 添加您需要的按钮
    • 设置单元格的标签

    然后把它还给我。

        2
  •  0
  •   Wolfgang Schreurs    15 年前

    谢谢,伙计,你的建议对我有用。对于任何感兴趣的人,我的[tableView:cellForRowAtIndexPath:]消息现在如下所示:

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    
        NSString *label = [[[controlArray objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]] objectForKey:@"text"];
        [[cell textLabel] setText:label];
    
        // TextField: USERNAME
        if ([[[[controlArray objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]] objectForKey:@"text"] isEqual:@"Username"]) {
            [[cell contentView] addSubview:username];
        }
    } else {
        NSString *label = [[[controlArray objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]] objectForKey:@"text"];
        [[cell textLabel] setText:label];
    
        // TextField: USERNAME
        if ([[[[controlArray objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]] objectForKey:@"text"] isEqual:@"Username"]) {
            [username removeFromSuperview];
            [[cell contentView] addSubview:username];
        }
    }  
    return cell;