首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >‘`didSelectRowAtIndexPath:’在调用‘`didSelectRowAtIndexPath:’之后未被调用

‘`didSelectRowAtIndexPath:’在调用‘`didSelectRowAtIndexPath:’之后未被调用
EN

Stack Overflow用户
提问于 2016-02-09 11:27:24
回答 3查看 487关注 0票数 0

我在viewDidAppear中这样称呼viewDidAppear

代码语言:javascript
复制
    -(void)viewDidAppear:(BOOL)animated {

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

}

我是didSelectRow

代码语言:javascript
复制
-(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不是未经接触而通过的。

代码语言:javascript
复制
-(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];
    }];
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-02-10 05:42:18

我想你应该告诉表视图,你是被选中的,在那之后,它第一次知道有一个单元格被选中了,所以你需要做下面这样的事情,

代码语言:javascript
复制
- (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
     }
  }

在上面,我们只需选择单元格,然后执行代码的动画部分(请参阅注释)。

为了简单起见,我将动画代码分开如下所示,

代码语言:javascript
复制
- (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;
        }];
    }];
}

以及下面这样的选举动画,

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

 }

您可以从tableview委托方法中计算这些方法,如下所示,

代码语言:javascript
复制
-(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];
}

而且u已经将单元格选择样式配置为UITableViewCellSelectionStyleNone

代码语言:javascript
复制
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {

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

Stack Overflow用户

发布于 2016-02-09 11:34:19

方法didSelectRowAtIndexPath是一个UITableViewDelegate方法。您不应该调用它,系统根据用户操作调用它。

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

代码语言:javascript
复制
[self.tableView selectRowAtIndexPath: [NSIndexPath indexPathForRow:0 inSection:0]];

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

票数 0
EN

Stack Overflow用户

发布于 2016-02-09 11:36:32

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

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

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

代码语言:javascript
复制
[tableView selectRowAtIndexPath:indexPath 
                       animated:NO 
                 scrollPosition:UITableViewScrollPositionMiddle];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35290867

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档