我有一个带卡片的UITableView
。每次我想在按下绘图按钮后添加新卡片时,我希望它从视图的中心移动到表中的位置,它应该用一些基本的动画放置。我设法使用以下代码获取新抽卡的目的地:
cellRectInTableDrawnCard = [[self playerCardsTable] rectForRowAtIndexPath:drawnCardIndexPath];
cellInSuperviewDrawnCard = [[self playerCardsTable] convertRect:cellRectInTableDrawnCard toView:[[self playerCardsTable] superview]];
但是,要确定cellRectInTableDrawnCard
我需要用reloadData
重新加载playerCardsTable
,但这已经显示了抽出的卡。这只是几分之一秒,因为我将新卡放在带有动画的表格中,该动画在reloadData
之后触发。动画后重新加载不是一种选择,因为我当时没有drawnCardIndexPath
。
有没有办法在不重新加载表视图的情况下获取矩形?否则,有没有办法在reloadData
后隐藏新单元格并在动画完成后显示它?
谢谢!
您可能希望插入行并单独填充它,而不是执行完整的表重新加载。
代码片段显示了一个按钮,该按钮使用 insertRowsAtIndexPaths:indexPathArray 添加一个新行,该行为您提供单元格矩形来执行动画操作。当你完成动画时,只需使用reloadRowsAtIndexPaths来填充单元格值(显示你是卡我猜)。
使用布尔值在cellForRowAtIndexPath中决定何时应该显示新卡(基本上是在调用reloadRowsAtIndexPaths之后)。
- (IBAction)butAddCardToHandAction:(id)sender {
// Add a blank record to the array
NSString *strCard = @"New Card";
_showCard = NO;
[_arrayHandCards addObject:strCard];
// create the index path where you want to add the card
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(_arrayHandCards.count - 1) inSection:0];
NSArray *indexPathArray = [NSArray arrayWithObjects:indexPath,nil];
// Update the table
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
// Ok - you got a blank record in the table, get the cell rect.
CGRect cellRectInTableDrawnCard = [[self tableView] rectForRowAtIndexPath:indexPath];
NSLog(@"My new rect has y position : %f",cellRectInTableDrawnCard.origin.y);
//Do the animation you need to do and when finished populate the selected cell
_showCard = YES;
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
控制单元格中显示的内容的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Set up the cell and use boolean to decide what to show
NSString *strToDisplayInCell;
if (!_showCard)
{
strToDisplayInCell = @"";
}
else
{
NSString *strToDisplayInCell = [_arrayHandCards objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
cell.textLabel.text = strToDisplayInCell;
}
return cell;
}