2014-04-06 65 views
0

我有一個UITableView卡。每次我想在按下繪圖按鈕後添加新卡片時,我都希望它從視圖的中心移動到表格中的位置,它應該放置一些基本的動畫。我已成功地獲得新的抽卡的目的,此代碼:Tableview reloadData和CGRect的新單元格

cellRectInTableDrawnCard = [[self playerCardsTable] rectForRowAtIndexPath:drawnCardIndexPath]; 
cellInSuperviewDrawnCard = [[self playerCardsTable] convertRect:cellRectInTableDrawnCard toView:[[self playerCardsTable] superview]]; 

然而,確定cellRectInTableDrawnCard我需要重新加載playerCardsTablereloadData但這已經顯示了繪製卡。這只是幾分之一秒,因爲我將新卡放在桌上,動畫片在reloadData之後觸發。在動畫之後重新加載不是一種選擇,因爲我沒有drawnCardIndexPath

有沒有一種方法可以得到rect而無需重新加載tableview?或者,有沒有辦法在reloadData之後隱藏新的單元格,並在動畫完成後顯示它?

謝謝!

回答

0

你可能想插入行和單獨填充它,而不是做一個完整的表格重新加載。

的代碼片段顯示使用insertRowsAtIndexPaths一個按鈕:indexPathArray添加一個新行,讓你的細胞RECT做你的動畫的東西。 當您完成動畫時,只需使用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; 
} 
+0

非常感謝您!我已經習慣了你的榜樣,並對它進行了微調,以適應我的願望,但它指出了我的正確方向。 – Niels