`didDeselectRowAtIndexPath:`在调用`didSelectRowAtIndexPath之后没有被调用:`

时间:2016-02-09 11:27:24

标签: ios uitableview

我在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;
        }];

    }];
}

它运行正常,但是当我选择一个新单元格时,viewDidAppear didSelectRow调用中选择的第一个单元格不会调用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];
    }];
}

3 个答案:

答案 0 :(得分:0)

方法didSelectRowAtIndexPathUITableViewDelegate方法。您不应该调用它,系统会根据用户操作调用它。

如果要选择表格视图行,则应调用表格视图的selectRowAtIndexPath方法:

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

如果您这样做,表格视图将正确选择行。

答案 1 :(得分:0)

您是否在UITableView的“属性”检查器中选中了“选择”选项? 是单选还是多选?

问题是由于多重选择 问题是由于多重选择

或者您可以在ViewWillApear中使用而不是didSelectRow,因为它不是在UITableView中选择行的正确方法

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

答案 2 :(得分:0)

我想你需要告诉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;
 }
相关问题