我試圖讓UISegmentedControl
一組UITableViewCell
中很像在設置應用程序的WiFi設置。我遇到的問題是我得到一個雙邊框。我爲UISegmentedControl
和UITableViewCell
獲得一個邊框。的UITableViewCell/UISegmentedControl邊界問題
我猜我需要從UITableViewCell
刪除邊框。我該怎麼做呢?
我試圖讓UISegmentedControl
一組UITableViewCell
中很像在設置應用程序的WiFi設置。我遇到的問題是我得到一個雙邊框。我爲UISegmentedControl
和UITableViewCell
獲得一個邊框。的UITableViewCell/UISegmentedControl邊界問題
我猜我需要從UITableViewCell
刪除邊框。我該怎麼做呢?
我剛剛注意到這仍然是答案。碰巧,我不得不爲另一個項目做這件事,因爲我問了這個問題,我學到了很多關於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:您提供的數字適用於肖像模式,但不適用於橫向模式。投票你:) – 2009-12-28 20:06:11
我這個有進一步輕微。到目前爲止,我已經分類了UITableViewCell。我創建了一個帶有UISegmentedControl的筆尖,並將UITableViewCell背景alpha設置爲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 SDK有UISegmentedControl的幾個例子在UICatalog示例代碼項目,他們有效地「欺騙」通過使用UIViewController和設置主視圖的背景顏色,以表格的背景色)
您引用的分段控制示例代碼使用通用視圖而不是表視圖來顯示其控件;它看起來像分組表格視圖,只是因爲它使用了背景顏色。 – 2009-12-25 00:53:22
挖入它並找到正確的答案。保留對我原來的答案的引用,以便其他人知道蘋果的例子實際上並沒有這樣做。 – 2009-12-28 20:05:17
在的Wi-Fi設置的情況下,我懷疑他們做了什麼作出了「忽略此網絡」按鈕,將「IP地址」標籤,而「DHCP/BOOTP /靜」,分段控制的一部分表格的標題視圖。如果你需要做到這一點在你的桌子中間(在頂部或底部,針對要分別使用tableHeaderView
和tableFooterView
性能相對),我建議使用委託方法-tableView:viewForHeaderInSection:
與-tableView:heightForHeaderInSection
,或相應的Footer
變體。對於其中的任何一個,您都會爲表格視圖的「部分」設置一個自定義視圖(使用清晰的背景色或[UIColor groupTableBackgroundColor]
),其中包含一個標籤和一個分段控件,以便它們與其餘部分匹配表格部分。
使用this post技術去除的UITableViewCell的背景透明度的工作更容易,我讓只有UISegmentedControl錶行中顯示。
我的解決方案是允許分段控件調整大小以適應並隱藏表格視圖的背景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;
}
A小調風格的建議:使用的適當的資本問題標題中的類名會使讀起來更容易。你可以在事後改變它。對不起,我沒有答案,我現在只做桌面應用程序。 – 2009-06-25 05:32:48