Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 带UISwitch的UITableView_Ios_Objective C_Iphone_Uitableview - Fatal编程技术网

Ios 带UISwitch的UITableView

Ios 带UISwitch的UITableView,ios,objective-c,iphone,uitableview,Ios,Objective C,Iphone,Uitableview,我使用故事板和自动布局。我用标签5将UISwitch添加到我的手机中。当我选择first UISwitch并向下滚动时,我看到另一个UISwitch也打开了,如果我向上滚动,我的第一个UISwitch将关闭。如何解决这个问题 我的代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellI

我使用故事板和自动布局。我用标签5将UISwitch添加到我的手机中。当我选择first UISwitch并向下滚动时,我看到另一个UISwitch也打开了,如果我向上滚动,我的第一个UISwitch将关闭。如何解决这个问题

我的代码:

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



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

    }


    UISwitch* switchView = (UISwitch *)[cell viewWithTag:5];

    [switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];

       return cell;

}

这是因为
UITableView
重用
UITableViewCell
因此一个单元格可以在不同的索引路径中使用多次,在这种情况下,您有责任维护
UITableViewCell
子视图的状态。更好的方法是
cellforrowatinexpath
返回单元格添加逻辑,以显示/隐藏
ui开关
或选择准确的状态,即打开或关闭,您可以在dataSource对象中保留该标志,然后可以检查该标志以使
UISwitch

每次调用cellForRowAtIndexPath时,都必须替换需要在该位置显示的单元格的特定数据。这包括标签、图像和UISwitch等内容

这是因为UITableView使用了少量可重用的单元格

在cellForRowAtIndexPath中添加如下内容:

switchView.on = [self isSwitchOnForCellAtIndexPath:indexPath]

然后写下确定开关是否应该打开所需的逻辑。

试试这个:

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

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        cell.textLabel.text = [self.settingsArray objectAtIndex:indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        if ([[self.settingsArray objectAtIndex:indexPath.row] isEqualToString:ROW_PRIVATE_BROWSING])
        {
            self.privateBrowsingSwitch =[[UISwitch alloc]initWithFrame:CGRectMake(cell.frame.size.width-65, 10, 30, 30)];
            if (ApplicationDelegate.privateBrowsing)
            {
                [self.privateBrowsingSwitch setOn:YES animated:YES];
            }
            [self.privateBrowsingSwitch addTarget:self action:@selector(changeSwitch:) forControlEvents:UIControlEventValueChanged];
            [cell addSubview:self.privateBrowsingSwitch];
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        return cell;
    }

这是因为单元格的可重用性……您需要在交换机上存储indexpath,并基于此填充单元格