2009-08-24 287 views
17

我創建了一個UITableView,並希望特定的UITableViewCell在加載視圖時顯示爲選中狀態(藍色)。默認情況下,如何選擇UITableViewCell?

+0

看到這個問題: http://stackoverflow.com/questions/19295297/uitableviewcell- set-selected-beginning/25128427#25128427 – 2015-06-14 04:56:05

回答

0

使用的UITableViewCell方法

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 

信息here

+0

也許下面的方法更合適? http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/occ/instm/UITableViewCell/setHighlighted:animated: – 2009-08-24 17:10:55

+0

哪種方法?鏈接不會去任何特定的方法 – Daniel 2009-08-24 17:27:16

+1

如果它ound,那一個突出顯示它,並不選擇它,我想如果這是所有提問者然後確定那更合適 – Daniel 2009-08-24 17:28:02

6

請謹慎使用此方法,因爲以這種方式選擇行Apple suggests against會顯示「選定」狀態。

相反,請考慮將單元格的accessoryType屬性設置爲UITableViewCellAccessoryCheckmark之類的內容。

+0

我想複製蘋果用戶界面時添加您添加/編輯日曆應用程序中的事件並設置開始和結束日期/時間。我在其他地方看到了關於複選標記的評論,但我不確定它是否適用於這種情況(表格用於佈置組件而不是呈現數據列表)。 – user119857 2009-08-24 19:55:40

+0

這是一個有趣的觀點。在這種情況下,它們不需要爲每個Start和End都有一個子視圖,因爲它們都代表相同類型的數據,並由同一種UIPickerView修改。所以這將是一個明智的用途。他們很可能使用UITableView的selectRowAtIndexPath:animated:scrollPosition:方法...請參閱http://tinyurl.com/yd9zs65 – 2009-10-07 19:38:11

1

絕對要小心。我相信你有一個很好的理由,但仔細看一下蘋果提供的人機界面指南文檔。應用因不取消選擇表格行而遭到拒絕。我鼓勵你找到HIG的適當部分,並看到蘋果提供任何建議。

41
-(void)viewWillAppear:(BOOL)animated { 
    // assuming you had the table view wired to IBOutlet myTableView 
    // and that you wanted to select the first item in the first section 
    [myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] 
          animated:NO 
         scrollPosition:UITableViewScrollPositionTop]; 
} 

我使用這種技術來選擇兩個UITableViewCells一個讓用戶知道哪個單元格的表格下方的UIDatePicker查看它會影響。當您創建新事件並設置日期時,蘋果會在日曆應用程序中使用此技術。

+1

1.這不適用於我。 2.蘋果說你應該使用複選標記來表示一個項目的選擇狀態。 – 2014-04-26 06:04:09

3

你應該把它放在viewWillAppear

[myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] 
         animated:NO 
        scrollPosition:0]; 

如果試圖在cellForRowAtIndexPath:選擇它,那麼它不會採取必要的風格。

0

有時,當您不在UIViewController中時,您沒有viewWillAppear,有時,您以編程方式創建了UITableView

最簡單的解決方案是實現此delegate方法:

- (void)tableView:(UITableView *)tableView 
     willDisplayCell:(UITableViewCell *)cell 
     forRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (self.selectedIndex == indexPath.row) { 
     cell.selected = YES; 
    } 
} 

這是因爲還沒有顯示單元在cellForRowAtIndexPath不起作用。並且僅在顯示這個方法時調用setSelected方法。

1

使用此代碼默認情況下,表視圖,選擇單元格,indexPath可以改變根據您的需要

-(void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0]; 
    [theTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom]; 
} 
相關問題