2009-05-22 98 views
6

我试图让UISegmentedControl一组UITableViewCell中很像在设置应用程序的WiFi设置。我遇到的问题是我得到一个双边框。我为UISegmentedControlUITableViewCell获得一个边框。的UITableViewCell/UISegmentedControl边界问题

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

+2

A小调风格的建议:使用的适当的资本问题标题中的类名会使读起来更容易。你可以在事后改变它。对不起,我没有答案,我现在只做桌面应用程序。 – 2009-06-25 05:32:48

回答

7

我刚刚注意到这仍然是答案。碰巧,我不得不为另一个项目做这件事,因为我问了这个问题,我学到了很多关于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]; 
+1

@ daniel-wood:您提供的数字适用于肖像模式,但不适用于横向模式。投票你:) – 2009-12-28 20:06:11

1

我这个有进一步轻微。到目前为止,我已经分类了UITableViewCell。我创建了一个带有UISegmentedControl的笔尖,并将UITableViewCell背景alpha设置为0.它看起来不太正确,但比以前更好。

0

诡计似乎是要将UISegmentedControl设置为控件的backgroundView的大小,而不是contentView的大小。我能够通过执行以下操作做到这一点编程:

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

请注意,如果您使用的是配件,你需要考虑的accessoryView为好。

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

  • self(在UITableViewCell或子类)
    • backgroundView
    • contentView
      • (你的控件放在这里)
    • accessoryView

在纵向布局,所述backgroundView的帧是{{9, 0}, {302, 44}},而contentView的帧是略小,在{{10, 1}, {300, 42}}。当表格样式分组时,这给单元格1px的“边框”。你必须同时调整的contentView你的控制,以获得适当的大小。

(注:虽然苹果实际上the SDKUISegmentedControl的几个例子在UICatalog示例代码项目,他们有效地“欺骗”通过使用UIViewController和设置主视图的背景颜色,以表格的背景色)

+0

您引用的分段控制示例代码使用通用视图而不是表视图来显示其控件;它看起来像分组表格视图,只是因为它使用了背景颜色。 – 2009-12-25 00:53:22

+0

挖入它并找到正确的答案。保留对我原来的答案的引用,以便其他人知道苹果的例子实际上并没有这样做。 – 2009-12-28 20:05:17

3

在的Wi-Fi设置的情况下,我怀疑他们做了什么作出了“忽略此网络”按钮,将“IP地址”标签,而“DHCP/BOOTP /静”,分段控制的一部分表格的标题视图。如果你需要做到这一点在你的桌子中间(在顶部或底部,针对要分别使用tableHeaderViewtableFooterView性能相对),我建议使用委托方法-tableView:viewForHeaderInSection:-tableView:heightForHeaderInSection,或相应的Footer变体。对于其中的任何一个,您都会为表格视图的“部分”设置一个自定义视图(使用清晰的背景色或[UIColor groupTableBackgroundColor]),其中包含一个标签和一个分段控件,以便它们与其余部分匹配表格部分。

2

使用this post技术去除的UITableViewCell的背景透明度的工作更容易,我让只有UISegmentedControl表行中显示。

1

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

这将产生的结果等同于“Settings.app>无线网络连接>您的网络> IP地址”分段控件没有硬编码任何布局指标:

- (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; 
}