【问题标题】:UITableViewCell / UISegmentedControl border issueUITableViewCell / UISegmentedControl 边框问题
【发布时间】:2010-10-28 04:37:08
【问题描述】:

我正在尝试在UITableViewCell 组中获取UISegmentedControl,就像在设置应用程序中的 wifi 设置中一样。我遇到的问题是我得到了双边框。我得到了一个UISegmentedControl 的边框和一个UITableViewCell 的边框。

我猜我需要从UITableViewCell 中删除边框。我该怎么做呢?

【问题讨论】:

  • 一个小的风格建议:在问题标题中使用正确的类名大写会使其更容易阅读。您可以在事后更改它。抱歉,我没有答案,我目前只做桌面应用程序。

标签: iphone objective-c uikit uitableview uisegmentedcontrol


【解决方案1】:

我对此有更进一步的了解。到目前为止,我已经将 UITableViewCell 子类化了。我创建了一个带有 UISegmentedControl 的笔尖,并将 UITableViewCell 背景 alpha 设置为 0。它看起来仍然不太正确,但比以前好多了。

【讨论】:

    【解决方案2】:

    诀窍似乎是将UISegmentedControl 的大小调整为控件的backgroundView 的大小,而不是contentView。我可以通过执行以下操作以编程方式做到这一点:

        // Size to cover the entire background
        self.contentView.frame = self.backgroundView.frame;
        self.myControl.frame = self.contentView.bounds;
    

    请注意,如果您使用的是配件,您还需要考虑accessoryView

    原因是视图层次结构如下:

    • selfUITableViewCell 或子类)
      • backgroundView
      • contentView
        • (您的控件在此处)
      • accessoryView

    在纵向布局中,backgroundView 的框架为{{9, 0}, {302, 44}},而contentView 的框架略小,为{{10, 1}, {300, 42}}。当表格样式被分组时,这为单元格提供了 1px 的“边框”。您必须同时调整contentView 您的控件的大小以获得适当的大小。

    (注意:虽然 Apple 实际上在 UICatalog 示例代码项目中的 UISegmentedControl 有几个示例,但它们通过使用 UIViewController 并将主视图的背景颜色设置为表格背景颜色。)

    【讨论】:

    • 您引用的分段控件示例代码使用通用视图而不是表格视图来显示其控件;它看起来像一个分组的表格视图,只是因为它使用了该背景颜色。
    • 深入研究并找到正确答案。保留对我旧答案的引用,以便其他人知道 Apple 的示例实际上并没有这样做。
    【解决方案3】:

    在 Wi-Fi 设置的情况下,我怀疑他们所做的是让“忘记此网络”按钮、“IP 地址”标签和“DHCP/BootP/Static”分段控制所有部分表的标题视图。如果您需要在表格中间执行此操作(而不是在顶部或底部,您将分别使用 tableHeaderViewtableFooterView 属性),我建议使用委托方法 @987654323 @ 与 -tableView:heightForHeaderInSection 或相应的 Footer 变体。使用其中任何一个,您都可以为表格视图的“部分”设置一个自定义视图(使用清晰的背景颜色或[UIColor groupTableBackgroundColor]),其中包含一个标签和一个分段控件,以便它们与表格的其余部分。

    【讨论】:

      【解决方案4】:

      我刚刚注意到这仍在得到答案。碰巧我不得不为另一个项目做这件事,自从我问了这个问题以来,我学到了更多关于 iPhone 开发的知识。这是我最近解决它的方法。这一切都是为了使框架具有正确的尺寸。这应该适用于标准表。

      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
      if(cell == nil) 
          cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"] autorelease];
      
      UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(-1.0f, -1.0f, 302.0f, 46.0f)];
      [cell.contentView addSubview:segmentedControl];
      

      【讨论】:

      • @daniel-wood:您提供的数字适合纵向模式,但不适用于横向模式。为此投票给你:)
      • 啊,真的。我想你可以为宽度做 tableView.frame.size.width - 18.0f 。所有其他值都应该相同。
      【解决方案5】:

      使用this post 中的技术去除 UITableViewCell 的背景不透明度对我来说更容易让 UISegmentedControl 只显示在表格行中。

      【讨论】:

      • 这是正确的做法——使控件的框架与cell.contentView.bounds相同,将autoresizingMask设置为UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight,将控件添加为cell.contentView的子视图并设置cell.backgroundView 转为透明的UIView
      【解决方案6】:

      我的解决方案是允许分段控件调整大小以适应,并在tableView:willDisplayCell:forRowAtIndexPath: 中隐藏表格视图的背景。

      这会产生与“Settings.app > WiFi > Your Network > IP Address”分段控制相同的结果,而无需对任何布局指标进行硬编码:

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          static NSString *CellIdentifier = @"Cell";
      
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
          if (cell == nil) {
              cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
          }
      
          UISegmentedControl *control = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"One", @"Two", @"Three", nil]];
          control.segmentedControlStyle = UISegmentedControlStylePlain;
          control.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
          control.frame = cell.contentView.bounds;
          [cell.contentView addSubview:control];
          [control release];
      }
      
      - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
          cell.backgroundView.alpha = 0.0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多