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

时间:2010-01-13 17:21:16

标签: iphone uitableview

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

我使用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:cellForRowAtIndexPath]消息将每个控件添加到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 个答案:

答案 0 :(得分:2)

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

你必须在代码中创建一个else分支,如果出列的可重用单元格不是nil:

  • 从单元格中删除所有按钮
  • 添加您需要的按钮
  • 设置单元格的标签

然后返回。

答案 1 :(得分:0)

谢谢你的伴侣,你的建议对我有用。对于任何感兴趣的人,我的[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;
相关问题