簡體   English   中英

在調用`didSelectRowAtIndexPath:`之后未調用`didDeselectRowAtIndexPath:`。

[英]`didDeselectRowAtIndexPath:` not being called after calling `didSelectRowAtIndexPath:`

我叫didSelectRowAtIndexPath:就像在viewDidAppear

    -(void)viewDidAppear:(BOOL)animated {

    [self tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

}

這是didSelectRow

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
    [UIView animateWithDuration:0.4 animations:^{

        cell.contentView.backgroundColor = [UIColor blackColor];
        cell.textLabel.textColor = [UIColor whiteColor];
    }];
    [UIView animateWithDuration:0.4 animations:^{
        UITableViewCell *cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
        cell.contentView.backgroundColor = [UIColor blackColor];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.4 animations:^{
            cell.textLabel.transform = CGAffineTransformIdentity;
        }];

    }];
}

它可以正常運行,但是當我選擇一個新的單元格時,在對didSelectRowviewDidAppear調用中選擇的第一個單元格不會調用didDeselectRowAtIndexPath:它保持黑色,並且在選擇新的單元格時,下面的動畫不會運行。 問題是在viewDidAppear中調用中選擇的第一個單元格保持選中狀態,直到再次選擇它為止。 似乎沒有通過觸摸就無法傳遞indexPath的問題。

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];

    [UIView animateWithDuration:0.4 animations:^{

        cell.contentView.backgroundColor = [UIColor whiteColor];
        cell.textLabel.textColor = [UIColor blackColor];
    }];
}

方法didSelectRowAtIndexPath是一個UITableViewDelegate方法。 您不應該調用它,系統會根據用戶操作來調用它。

如果要選擇表視圖行,則應調用表視圖的selectRowAtIndexPath方法:

[self.tableView selectRowAtIndexPath: [NSIndexPath indexPathForRow:0 inSection:0]];

如果這樣做,表視圖將正確選擇該行。

您是否在UITableView的屬性檢查器中選中了Selection選項? 是單選還是多選?

問題是由於多重選擇問題是由於多重選擇

或者您可以在ViewWillApear中使用而不是didSelectRow,因為這不是在UITableView中選擇行的正確方法

[tableView selectRowAtIndexPath:indexPath 
                       animated:NO 
                 scrollPosition:UITableViewScrollPositionMiddle];

我認為您需要告訴tableview您已經選擇了一個單元格,之后才第一次知道已經選擇了一個單元格,為此,您需要執行以下操作,

- (void)viewDidAppear:(BOOL)animated
   {
     [super viewDidAppear:animated];
     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
     UITableViewCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath];
     if(cell)
     {
        //tell the tableview that first cell is selected so that deselct method is called after selecting the otehr cell
        [self.myTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        [self performSelectionAnimationFor:cell]; //perfor your selection animation
     }
  }

在上面,我們只是選擇單元格,然后執行代碼的動畫部分(請參見注釋)。

為簡單起見,我分離了如下的動畫代碼,

- (void)performSelectionAnimationFor:(UITableViewCell *)cell
{
    [UIView animateWithDuration:0.4 animations:^{

        cell.contentView.backgroundColor = [UIColor blackColor];
        cell.textLabel.textColor = [UIColor whiteColor];
    }];

    [UIView animateWithDuration:0.4 animations:^{
        //UITableViewCell *cell = (UITableViewCell*)[self.myTableView cellForRowAtIndexPath:indexPath];
        cell.contentView.backgroundColor = [UIColor blackColor];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.4 animations:^{
            cell.textLabel.transform = CGAffineTransformIdentity;
        }];
    }];
}

並取消選擇動畫,如下所示,

 - (void)performDeselectionAnimationFor:(UITableViewCell *)cell
 {
     [UIView animateWithDuration:0.4 animations:^{
         cell.contentView.backgroundColor = [UIColor whiteColor];
         cell.textLabel.textColor = [UIColor blackColor];
     }];

 }

您可以從如下的tableview委托方法中存儲這些方法,

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     UITableViewCell *cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
     [self performSelectionAnimationFor:cell];
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
   [self performDeselectionAnimationFor:cell];
}

並且您已經將單元格選擇樣式配置為UITableViewCellSelectionStyleNone

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

    //...cell creation 
    //set this if u are not 
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
 }

暫無
暫無

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

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