【问题标题】:In UITableViewCell make UISwitch get the click then accesoryView if switch is on在 UITableViewCell 中,如果开关打开,则 UISwitch 获得点击,然后是 accesoryView
【发布时间】:2011-11-17 17:50:21
【问题描述】:

我正在尝试构建一个如下所示的 UITableViewCell:

由于我还不能发布图片,我将尝试通过说它是一个标签(左)与一个 UISwitch(中)和附件(右)来描述它。

希望你能得到照片...

这个想法是,如果开关关闭,accessoryView 是可见的,但禁用。当用户打开开关时,他们可以点击并向右导航以查看他们可以选择的选项列表。问题是,当开关被轻敲时,细胞得到的是轻敲而不是开关。

我该怎么办? (使开关先得到水龙头)。我猜这是 firstResponder 的事情,但我没有找到我需要的魔法代码。

一旦我解决了这个问题,我可能会自己弄清楚附件的启用/禁用...

谢谢。

【问题讨论】:

    标签: ios uitableview uiswitch custom-cell first-responder


    【解决方案1】:

    创建 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] autorelease];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.accessoryType = (UITableViewCellAccessoryNone;
                UISwitch* aSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
    aSwitch.tag = [indexPath row];
    
                CGRect rect = cell.frame;
                CGRect switchRect = aSwitch.frame;
                switchRect.origin = CGPointMake( (rect.size.width / 2) - (aSwitch.frame.size.width / 2), 
                                                   (rect.size.height / 2) - (aSwitch.frame.size.height / 2));
    
                aSwitch.frame = switchRect;
                [aSwitch addTarget:self action:@selector(switchSwitched) forControlEvents:UIControlEventValueChanged];
                [cell addSubview:aSwitch];
    
    
                [aSwitch release];
            }
    
            return cell;
        }
    
        - (void)switchSwitched:(UISwitch*)sender {
    
            if (sender.on) {
    
        UITableViewCell* aCell = [self.tableView cellForRowAtIndexPath:
                                  [NSIndexPath indexPathForRow:sender.tag inSection:0]];
    
        aCell.accessoryType = (sender.on == YES ) ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
            }
        }
    

    您也可以通过继承 UITableViewCell 并添加 UITableViewCell nib 文件来实现这一点。 使 UIViewTableController 成为 Cell nib 文件的文件所有者,向 UIViewController 添加子类单元的 IBOutlet。使用

    加载自定义单元格
            [[NSBundle mainBundle] loadNibNamed:@"Your Custom Cell nib file name" owner:self options:nil];
    

    请参阅 Apple 的 iOS 编程指南http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      • 2011-06-05
      • 2015-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多