2011-06-14 111 views
6

我完全不知道爲什麼我的配件視圖不工作。我只想讓一些文本出現在UITableViewCell的右側(以及左側),但只顯示左側的文本。UITableViewCell accessoryView不顯示

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

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"SwitchCell"]; 

    if (cell==nil){ 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"SwitchCell"] autorelease]; 
     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 60, 30)]; 

     cell.textLabel.text = @"left text"; 
     label.text = @"right text"; 

     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

     cell.accessoryView = label; 
     [label release]; 

    } 
    return cell; 
} 

任何想法?謝謝。

+0

我建議你先給標籤對齊。默認情況下,標籤對齊必須保留。因此,兩個文本可能會成爲問題。然後你可以說:[cell.contentView addSubView:label]; – 2011-06-14 13:25:46

回答

24
cell.accessoryView = label; 

你設置你的accessoryView的是一個標籤,所以你不會看到任何披露指標。如果你想在你的標題和詳細的文字,然後初始化它UITableViewCellStyleValue2像這樣...

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"cellType"] autorelease]; 

...然後設置類似這樣的標題和細節......

cell.textLabel.text = @"Title"; 
cell.detailTextLabel.text = @"Description"; 

要得到的不同的內置表格樣式的想法檢查下面的圖片...

enter image description here

可以調整爲textLabel和detailTextLabel送滿足你的口味。

+0

雖然這不直接回答我的問題(我有一個UILabel作爲附件在我的應用程序的其他地方工作,所以我不知道爲什麼它不在這裏工作),你肯定爲我提供了一些很好的選擇。我所關心的主要問題是文本被截斷。有沒有辦法解決這個問題(除非我用我自己的自定義單元格)? – 2011-06-14 14:47:49

+0

我認爲這些替代方案是正確的選擇。 – 2011-06-22 09:36:39

+0

謝謝,這解決了我的問題! – 2012-09-19 12:51:11

6

這是爲什麼?你不能擁有兩個。

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
+0

是的。我的錯。但是,如果你拿出那行代碼,我的問題仍然存在。 – 2011-06-15 08:47:13

3
-(id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier 

中的iOS 3.0

是貶值而不是使用:

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 

通在任一UITableViewCellStyleValue1UITableViewCellStyleValue2並根據需要設置textLabeldetailTextLabel性能。

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html%23//apple_ref/doc/uid/TP40006938-CH3-SW34

0

UITableViewCellStyleValue2給了我一個奇怪的風格。我認爲最常用的風格是UITableViewCellStyleValue1(至少在我的情況下)。

0

還要記住:如果您有自定義附件視圖,那麼accessoryType屬性將被忽略。所以,這行代碼是多餘的。

相關問題