我在viewDidAppear中这样称呼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];
}];
}发布于 2016-02-10 05:42:18
我想你应该告诉表视图,你是被选中的,在那之后,它第一次知道有一个单元格被选中了,所以你需要做下面这样的事情,
- (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];
}而且u已经将单元格选择样式配置为UITableViewCellSelectionStyleNone。
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//...cell creation
//set this if u are not
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}发布于 2016-02-09 11:34:19
方法didSelectRowAtIndexPath是一个UITableViewDelegate方法。您不应该调用它,系统根据用户操作调用它。
如果要选择表视图行,则应调用表视图的selectRowAtIndexPath方法:
[self.tableView selectRowAtIndexPath: [NSIndexPath indexPathForRow:0 inSection:0]];如果这样做,表视图将正确选择行。
发布于 2016-02-09 11:36:32
您是否在UITableView的属性检查器中选中了选择选项?是单选还是多选?
问题是由于多重选择,问题是由于多重选择。
或者您可以在ViewWillApear中使用而不是didSelectRow,因为在UITableView中选择行不是一种正确的方法。
[tableView selectRowAtIndexPath:indexPath
animated:NO
scrollPosition:UITableViewScrollPositionMiddle];https://stackoverflow.com/questions/35290867
复制相似问题