【问题标题】:UISwitch changes cell when scrolling tableviewUISwitch 在滚动表格视图时更改单元格
【发布时间】:2011-04-08 10:49:33
【问题描述】:

我有一个奇怪的问题(至少在我看来)。当我将 UISwitch 添加到我的表格视图时,当用户滚动表格视图时,开关会自动更改单元格。下面是我如何为 tableview 创建 UISwitch 的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        //NSLog(@"This is what we are looking for %@    %@", checkUsername, checkPassword);
        // Configure the cell...
    NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
    NSArray *array =[dictionary objectForKey:@"Settings"];
    NSString * cellValue = [array objectAtIndex:indexPath.row];

    static NSString *CellIdentifier = @"Cell";
    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.accessoryView = nil;


    if (!cell) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.adjustsFontSizeToFitWidth = YES;
            //switcher.on = [[NSUserDefaults standardUserDefaults]boolForKey:@"Settings"];

        if (section == 1)
        {

            UISwitch *switchView = [[UISwitch alloc] init];
            cell.accessoryView = switchView;
            [switchView setOn:YES animated:NO];
            [switchView setTag:[indexPath row]];
            [switchView addTarget:self action:@selector(switchWasChangedFromEnabled:) forControlEvents:UIControlEventValueChanged];
                //[self.tableView reloadData];
            [switchView release];
        }

    }
    return cell;
}

谁能告诉我为什么开关总是移出指定的单元格以及可能的补救措施。

贾斯汀·加拉格尔建议:

发生这种情况是因为您正在缓存单元格并重复使用它们。有时,带有开关的单元会在另一个地方重复使用。尝试使用开关为单元格设置不同的标识符。

但是谁能告诉我如何为每个单元格设置不同的标识符?

干杯,

iSee

【问题讨论】:

    标签: objective-c uitableview uiswitch


    【解决方案1】:

    发生这种情况是因为您正在缓存单元格并重复使用它们。有时带有开关的单元会在另一个地方重复使用。尝试使用开关为单元格设置不同的标识符。

    【讨论】:

    • NO.. 这不是.. 这是因为 UISwitch 正在 cellForRowAtIndexPath 方法中初始化。所以 UISwitch 将自己重置回初始状态。请在 .h 文件中声明您的 switchView 变量,然后在 viewDidLoad 期间对其进行初始化,然后将您的 switchView 添加到您的附件视图中。这将解决问题
    【解决方案2】:

    我使用here [原始来源] 提供的以下代码来解决我的问题。我在下面粘贴了代码 sn-p。此外,这个想法是贾斯汀加拉格尔给我的。谢谢。

    - (void)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        if (<indexPath is for cell type "A">) {
        static NSString *SomeIdentifierA = @"SomeIdentifierA";
    
            // This could be some custom table cell class if appropriate    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SomeIdentifierA];
        if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SomeIdentifierA] autorelease];
                // Any other attributes of the cell that will never change for type "A" cells.
        }
    
            if (someCondition) {
                cell.textColor = <someColor>;
            } else {
                cell.textColor = <anotherColor>;
            }
            cell.text = <some text>;    
        return cell;
        } else if (<indexPath is for cell type "B") {
        static NSString *SomeIdentifierB = @"SomeIdentifierB";
    
            // This could be some custom table cell class if appropriate    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SomeIdentifierB];
        if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SomeIdentifierB] autorelease];
                // Any other attributes of the cell that will never change for type "B" cells.
        }
    
            cell.text = <some text>;    
        return cell;
        } else {
            return nil; // Oops - should never get here
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-18
      • 1970-01-01
      相关资源
      最近更新 更多