iphone - 如何正确更新表中部分中动态变化的行数(及其单元格内容)?

标签 iphone uiswitch uitableview

我正在尝试创建一个 UITableViewController 子类,为我提供 UITableView 和我的应用程序设置。我在重新加载有关 UISwtitch 状态变化的数据时遇到问题。

该表有 3 个部分:

section 1 - one row constantly

section 2 - UISwitch in the first row making this section consist of 1 or 3 rows (depending on the state of UISwitch)

section 3 - UISwitch in the first row making this section consist of 1 or 3 rows (depending on the state of UISwitch), but with another UISwitch and additional rows data.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    switch ( section )
    {
        case 0:
            return 1;
            break;
        case 1:
            if ( limit_password_attempts )
                return 3;
            else 
                return 1;
            break;
        case 2:
            if ( lock_when_inactive )
                return 3;
            else 
                return 1;
            break;
        default:
            return 1;
            break;
    }  
}

第二和第三部分第一行中的两个 UISwitch-s 分别正在更改 BOOL

BOOL limit_password_attempts;
BOOL lock_when_inactive;

我的问题是,当我在第二部分中切换 UISwitch(例如)时,我希望第二部分再扩展 2 行,并填充新数据。它延伸了,但新的细胞看起来并不像我想要的那样。第二行成为下一节中第一行的副本(第三节带有 UISwitch 的行),如果第三节之前未扩展,则第三行正确返回,如果是,则该行成为第三部分第二行的副本。

所以我对单元格的定制看起来像这样,我猜错误就在这里。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
    UITableViewCell *cell = nil;
    static NSString *EnabledCellIdentifier = @"Enabled";
    cell = [tableView dequeueReusableCellWithIdentifier:EnabledCellIdentifier];
    
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] 
                   initWithStyle: UITableViewCellStyleDefault 
                 reuseIdentifier: EnabledCellIdentifier]
                    autorelease];
    
        cell.detailTextLabel.enabled = YES;
        
        switch ( indexPath.section )
        {
            case 0: // section 1
                cell.textLabel.text = @"Banks settings";
                break;
            case 1: // section 2
            {
                switch ( indexPath.row )
                {
                    case 0:
                    {
                        cell.textLabel.text = @"Limit passwords attempts";
                        UISwitch* actSwitch = [[UISwitch alloc] initWithFrame: CGRectZero ];
                        
                        [cell setEditingAccessoryView: actSwitch];
                        
                        [actSwitch setTag: indexPath.section];
                        [actSwitch addTarget: self
                                      action: @selector(actSwitchChanged:) 
                            forControlEvents: UIControlEventValueChanged];
                        
                        [self.view addSubview:actSwitch];
                        [actSwitch setOn:YES animated:NO];
                        [actSwitch setOn:NO animated:NO];
                        
                        actSwitch.on = NO;
                        [cell addSubview: actSwitch];
                        cell.accessoryView = actSwitch;
                        [actSwitch release];
                        break;
                    }
                    case 1:
                        cell.textLabel.text = @"Lock after 3 atempts";
                        break;
                    case 2:
                        cell.textLabel.text = @"Lock for 30 min";
                        break;
                    default:
                        break;
                }
                break;
            }
                
            case 2: // section 3
            {
                switch ( indexPath.row )
                {
                    case 0:
                    {
                        cell.textLabel.text = @"Lock when inactive";
                        UISwitch* pactSwitch = [[UISwitch alloc] initWithFrame: CGRectZero ];
                        
                        [cell setEditingAccessoryView: pactSwitch];
                        
                        [pactSwitch setTag: indexPath.section];
                        [pactSwitch addTarget: self
                                      action: @selector(actSwitchChanged:) 
                            forControlEvents: UIControlEventValueChanged];
                        
                        [self.view addSubview:pactSwitch];
                        [pactSwitch setOn:YES animated:NO];
                        [pactSwitch setOn:NO animated:NO];
                        
                        pactSwitch.on = NO;
                        [cell addSubview: pactSwitch];
                        cell.accessoryView = pactSwitch;
                        [pactSwitch release];
                        
                        
                        break;
                    }
                    case 1:
                        cell.textLabel.text = @"Lock when inactive for 20 min";
                        break;
                    case 2:
                        cell.textLabel.text = @"Unlock key";
                        break;
                    default:
                        break;
                }
                break;
            }
            default:
                NSLog(@"%d", indexPath.section );
                break;
        }
    }
    return cell;
}

看起来有一些隐藏的数组包含行数据,正在填充 onec,并且表 Controller 以某种方式更新表,具体取决于出现的行,仅更新它们。在节中添加行时为什么会生成副本。这就像在数组中插入一个节点。我认为它必须插入细胞前进,而不仅仅是复制。 我必须以某种方式删除行才能更新这种表吗? 我想我在这里缺少一些基本的东西。一些 MVC 概念?但这里真的有必要有数据模型吗?为什么?如果是的话我该怎么做?

感谢您的回答。 祝你有美好的一天。

<小时/>

它在我的代码中。我只是没有在我的问题中写下来。 UISwtitch 切换后调用 reloadData 方法:

[pactSwitch setTag: indexPath.section];
[actSwitch addTarget: self
              action: @selector(actSwitchChanged:) 
    forControlEvents: UIControlEventValueChanged];

在 actSwitchChanged 方法中:

-(void) actSwitchChanged: (UISwitch*) pSwitch {
      
    if ( [pSwitch tag] == 1 ) //section 2
        limit_password_attempts = !limit_password_attempts ;
    else if ( [pSwitch tag] == 2 ) //section 3
        lock_when_inactive = !lock_when_inactive;
    [self.tableView reloadData];

}

所以 reloadData 确实改变了 viewTable 的内容,但它使它不像我期望的那样。

有什么想法吗?

最佳答案

数据模型始终存在...即使它只是 UITableView 调用的一堆 if 语句。我认为这种区别导致了问题。

您的 bool 值代表您的基础数据模型。与数据模型的所有更改一样,您必须让 TableView 知道基础数据何时发生更改。

每当您更改 bool 值之一时,请尝试添加以下调用。

[myTableViewController.tableView reloadData];

关于iphone - 如何正确更新表中部分中动态变化的行数(及其单元格内容)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4871953/

相关文章:

ios - iOS7 中可能存在 UISwitch 错误?

ios - 如何在 UITable 中获取 UIButton 或 UILabel 的位置?

ios - 在 UITableViewCell 中设置 iOS 通讯簿图片

iphone - 如何相对于用户握持设备的方式绕 Y 轴旋转?

ios - 使用 iPhone 将 mac 检测为 iBeacon

iphone - layer.zPosition 不适用于非同级 UIView

iPhone: "initWithNibName:bundle:"的 TableViewController nibName 错误

swift - 在自定义单元格中使用 UISwitch 设置对象属性

Android 相当于 UISwitch

ios - 在 Swift 中,从 JSON 加载的 tableView 数据仅在 80% 的时间内加载