【问题标题】:How to create a UITableView full of UITableViewCells each with a UISwitch?如何创建一个充满 UITableViewCells 的 UITableView,每个都有一个 UISwitch?
【发布时间】:2013-06-26 21:02:16
【问题描述】:

我正在制作电话簿。

我正在尝试创建一个 UITableView,其中包含一个原型单元格,其中包含一个 UISwitch,它描述了特定联系人是公开的还是私有的。

如何创建这样的 UISwitch 即使在滚动期间也能保持其切换状态? dequeueReusableCellWithIdentifier 给我带来了很多问题,因为它没有保存 UISwitches 的切换状态。

虽然我喜欢节省内存,但切换 cellForRowAtIndexPath 中的开关(根据联系人的隐私布尔值)无法始终显示联系人的公共/私人状态。

请参阅下面我正在尝试构建的屏幕截图:

编辑:这是我的 cellForRowAtIndexPath:

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

    AHBContact *thisContact = nil;

    AHBBrowseContactCell *cell  =[tableView dequeueReusableCellWithIdentifier:@"BROWSECELL"];

    // from  contacts list
    thisContact = [self.contacts[indexPath.section] allValues][0][indexPath.row];

    cell.customPublicSwitch.onTintColor = [AHBUtilities greenColor];
    cell.customPublicSwitch.onText= @"Public";
    cell.customPublicSwitch.offText=@"Private";

    if(thisContact.privacy){
        NSLog(@"public %@",thisContact.fullName);
       cell.customPublicSwitch.on = YES;

    }
    else{
        NSLog(@"private %@", thisContact.fullName);

       cell.customPublicSwitch.on = NO;

    }

    cell.labelName.text = thisContact.fullName;
    cell.labelName.font = [AHBUtilities regularFontWithSize:cell.labelName.font.pointSize];
    cell.imageViewIcon.image = [AHBIcons phoneIconForCategory:thisContact.category];

    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor colorWithWhite:1 alpha:0.55];

    return cell;
}

第二次编辑:这是我在开关的“valueChanged”上运行的“togglePublic”方法

    -(IBAction)togglePublic:(UIControl *)button withEvent:(UIEvent *)event {

        DCRoundSwitch *switch1 = (DCRoundSwitch *)button;
        UITableViewCell *cell = (UITableViewCell *)switch1.superview;
        NSIndexPath *indexPath = [self.tableview indexPathForCell: cell.superview];

        if ( indexPath == nil ){

            return;

        }

        AHBContact *contact = [self.contacts[indexPath.section] allValues][0][indexPath.row];

        contact.privacy = !contact.privacy;

        NSLog(@"Public switch toggled for: %@", contact.firstName);

        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

[[AHBContactsController sharedController] updateContact:contact completion:^(id result) {

        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];


           NSLog (@"Public/Private updated%@",result);


       } failure:^(NSError *error) {

           [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

           UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Sync Problem"
                                                             message:@"Could save changes. Please try again."
                                                            delegate:self
                                                   cancelButtonTitle:@"Ok"
                                                   otherButtonTitles:nil];
           [message show];



       }];

    }

【问题讨论】:

  • cellForRow... 中设置开关的状态是一种有效的方法。如果它不起作用,那么你做错了什么。发布您的cellForRow... 方法。
  • @rmaddy 刚刚添加了编辑——让我知道你的想法!
  • 代码显示正确。也许问题在于处理正在更改的开关值。您是否有代码响应用户更改的开关,然后用新值更新正确的AHBContact 记录?
  • @rmaddy 添加了另一个编辑以显示我在开关的“valueChanged”上运行的切换方法
  • 您是否验证过togglePublic:withEvent: 方法实际上是在用户切换开关时调用的?如果是,它是否更新了 table view 的数据源使用的相同数据对象?

标签: ios uitableview uiswitch


【解决方案1】:

尝试在委托方法中设置

- (void)tableView:(UITableView *)tableView willDsiplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    UISwitch *switch = cell.yourCellSwitch;
    switch.on = yourSwitchVariable;
}

【讨论】:

  • 刚试过这个,当我滚动时没有工作。基本上,开关切换,但随后是开关/单元被其他单元随机重用(结果显示不正确的公共/私人信息)
【解决方案2】:

也许您在 cellForRowAtIndexPath: 中创建的开关是问题所在。尝试这样做。使用 UILabel 和 UISwitch 创建自定义单元格。制作这些 IBOutlets 和自定义单元格的属性并将它们连接起来。现在在 cellForRowAtIndexPath: 方法中使用自定义单元格而不是默认单元格。由于单元格正在被重用,并且每次当单元格在屏幕上可见时都会调用此方法,我猜在您每次创建 UISwitch 然后分配其状态时会发生不一致

    static NSString *cellIdentifier = @"CellIdentifier";

        YourCustomCell *cell = (YourCustomCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell)
        {
            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"YourCustomCell" owner:self options:nil];
            for (id currentObject in topLevelObjects){
                if ([currentObject isKindOfClass:[UITableViewCell class]]){
                    cell =  (YourCustomCell *) currentObject;
                    break;
                }
            }
        }

       // from  contacts list
            thisContact = [self.contacts[indexPath.section] allValues][0][indexPath.row];
    if(thisContact.privacy){
            NSLog(@"public %@",thisContact.fullName);
           cell.customPublicSwitch.on = YES;

        }
        else{
            NSLog(@"private %@", thisContact.fullName);

           cell.customPublicSwitch.on = NO;

        }

        cell.labelName.text = thisContact.fullName;
        cell.labelName.font = [AHBUtilities regularFontWithSize:cell.labelName.font.pointSize];
        cell.imageViewIcon.image = [AHBIcons phoneIconForCategory:thisContact.category];
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.backgroundColor = [UIColor colorWithWhite:1 alpha:0.55];
     return cell;
}

希望这会有所帮助:)

【讨论】:

  • 刚试过这个,似乎对我不起作用——即使我删除了同样的问题 "AHBBrowseContactCell cell = (AHBBrowseContactCell)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; "完全。它甚至没有出队,并且单元格仍然被弄乱了(但这次它们只是被重置为默认状态)!这怎么可能??另外,我注意到将我的 UISwitch 设置为 on (setOn) 会触发“valueChanged”并触发更新......这可能是个问题吗?
  • 你能粘贴你当前的代码吗?不看代码我不能告诉你确切的原因
猜你喜欢
  • 1970-01-01
  • 2022-07-29
  • 2018-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-12
  • 2014-08-03
  • 2012-12-02
相关资源
最近更新 更多