簡體   English   中英

選擇UITableView上的所有單元格

[英]Select all cells on UITableView

我正在嘗試使用編輯按鈕選擇表格中的所有單元格。 我一直想弄清楚我做錯了什么。 這是我的代碼。 我基本上循環遍歷各個部分,然后遍歷行數。 無論我做什么,它都沒有設置選擇每個細胞。 我怎么做到這一點?

@property (strong, nonatomic) IBOutlet UITableView *caseDataTableView;

@synthesize segmentControl,caseDataTableView,IndicationView,procedureView,indicationScrollView,procedureScrollView,segmentControllView,segmentControllios6;


- (IBAction)editButtonTapped:(id)sender {

    for (int i = 0; i < self.caseDataTableView.numberOfSections; i++) {
        for (NSInteger r = 0; r < [self.caseDataTableView numberOfRowsInSection:i]; r++) {
             UITableViewCell *cell = [self.caseDataTableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:r inSection:i]];
             [cell setSelected:YES];
        }
    }
}

你可以使用for循環

- (IBAction)editButtonTapped:(id)sender 
{
   for (int i = 0; i < self.tableView.numberOfSections; i++) 
   {
       for (int j = 0; j < [self.tableView numberOfRowsInSection:i]; j++)    
       {
        [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:j inSection:i]
                                    animated:NO
                              scrollPosition:UITableViewScrollPositionNone];
       }
   }
}

您可以選擇一個單元格調用表視圖的selectRowAtIndexPath方法:

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

我遇到的問題是兩件事。 第一個是editButtonTapped方法不正確。

- (IBAction)editButtonTapped:(id)sender {

for (int i = 0; i < self.caseDataTableView.numberOfSections; i++) {
    for (NSInteger r = 0; r < [self.caseDataTableView numberOfRowsInSection:i]; r++) {
        [self tableView:caseDataTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:i]];
    }
}}

下一個問題是當我進行循環時,didSelectRowAtIndexPath方法向下滾動。 它最終會下到前一個循環中的最后一個單元格。 我在didSelectRowAtIndexPath方法中取出了這段代碼。

[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]

在我的情況下設置scrollPosition將清除選擇,這就是它被刪除的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM