簡體   English   中英

突出顯示長按的表格視圖單元格,並使用Objective-C在iOS中取消突出顯示

[英]Highlight long pressed table view cell and unhighlight in iOS using objective-C

長按突出顯示表格視圖單元格。

我提到了鏈接

長按UITableView

長按效果良好,但表格視圖單元格未突出顯示。 所以我將以下行添加到handleLongPress方法

[self.myTableView selectRowAtIndexPath:indexPath animated:NO  scrollPosition:UITableViewScrollPositionNone];

在長按單元格突出顯示之后,還應滿足以下條件,

  1. 觸摸同一長按的表格單元格應該取消突出顯示,就像再次觸摸表格單元格一樣。
  2. 觸摸另一個表格視圖單元格應該像選擇多個單元格一樣突出顯示。
  3. 在第二次觸摸其他表格視圖單元格時,它們應取消突出顯示。

長按的行為應類似於觸摸表格單元格,但不應該是實際的觸摸功能。 請指導我。

嘗試:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

請使用此類邏輯。 當您開始長按時,將顯示選定的行,而長按結束時則必須取消選擇行。

-(IBAction)longPressBegan:(UILongPressGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateBegan)
    {
        // Long press detected, Just highlight row 
    }
    else
    {
        if (recognizer.state == UIGestureRecognizerStateCancelled
            || recognizer.state == UIGestureRecognizerStateFailed
            || recognizer.state == UIGestureRecognizerStateEnded)
        {
            // Long press ended, deselect row
        }
    }
}

希望這會幫到你...........

因此,我認為您的所有條件僅在您長時間按下某個單元格的情況下才會發生。 看來您可以進行長時間的宣傳。 所以現在

  1. 在您的課程中聲明一個BOOL。 BOOL是重點人物;
  2. 在長按事件中添加

     [self.myTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; YourCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath]; //I dont't know your model for this tableview so I am using cell tag for it //using 1 for selected //2 for unselected // 3 for long pressed one cell.tag = 3; isHiglighted = YES; 
  3. 在didSelectRowAtIndexPath中使用以下sudo代碼

     - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if(isHiglighted) { YourCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath]; if(cell.tag==3) { [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; cell.tag = 2; isHiglighted = NO; } else if(cell.tag==1) { [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; cell.tag = 2; } else { [self.myTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; cell.tag = 1; } } else { // do your stuff ofr single tap if user never long pressed } } 

暫無
暫無

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

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