如果我做insertRowsAtIndexPaths UITableView闪烁

我正在使用insertRowsAtIndexPaths方法将行添加到UITableView 。 我把这个代码放在[myTable beginUpdates];[myTable endUpdates]; 。 问题是我的表闪烁,然后滚动到最后一行?

可能是什么问题? 如果我评论[myTable beginUpdates];[myTable endUpdates]; :那么它工作正常。

提前致谢 !!!

这是我的代码:

 #pragma mark - NSFetchResultController - (void)setFetchedResultsController:(NSFetchedResultsController*)fetchedResultsController { _fetchedResultsController = fetchedResultsController; fetchedResultsController.delegate = self; [fetchedResultsController performFetch:NULL]; } - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { [tblComments beginUpdates]; } - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { switch (type) { case NSFetchedResultsChangeInsert: { [tblComments insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; postUpdateScrollTarget = newIndexPath; break; } case NSFetchedResultsChangeDelete: { [tblComments deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; break; } case NSFetchedResultsChangeUpdate: { [self configureCell:(LFMCommentCell *)[tblComments cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; //[tblComments reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; break; } case NSFetchedResultsChangeMove: { [tblComments deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tblComments insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; } } } - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { [tblComments endUpdates]; if (postUpdateScrollTarget) { [tblComments scrollToRowAtIndexPath:postUpdateScrollTarget atScrollPosition:UITableViewScrollPositionBottom animated:NO]; } postUpdateScrollTarget = nil; } 

我不明白这个问题,如果问题是如何插入/删除表视图的单元格,我们必须记住:更新后现有节中包含的行数必须等于包含在更新前的那个部分,加上或减去从该部分插入或删除的行数

例如:

 class FruitsTableViewController: UITableViewController { var fruits = ["Apple", "Banana", "Orange", "Pear", "Cherry"] // MARK: View LifeCycle override func viewDidLoad() { super.viewDidLoad() } override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) // delete one cell randomly one seconde after the view appear NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "deleteOneCellRandomly", userInfo: nil, repeats: false); // add on cell in the top after 2 secondes NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: "addOneCellInTheTop", userInfo: nil, repeats: false); } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } // MARK: - managing TableView cells func deleteOneCellRandomly() { let rowIndex = Int(arc4random_uniform(UInt32(fruits.count))) let indexPathToDelete = NSIndexPath(forRow: rowIndex, inSection: 0) tableView.beginUpdates() fruits.removeAtIndex(rowIndex) tableView.deleteRowsAtIndexPaths([indexPathToDelete], withRowAnimation: UITableViewRowAnimation.Automatic) tableView.endUpdates() } func addOneCellInTheTop() { let rowIndex = 0 let indexPathToDelete = NSIndexPath(forRow: rowIndex, inSection: 0) tableView.beginUpdates() fruits.insert("Grape", atIndex: rowIndex) tableView.insertRowsAtIndexPaths([indexPathToDelete], withRowAnimation: UITableViewRowAnimation.Automatic) tableView.endUpdates() } // MARK: - Table view data source override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return fruits.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell cell.textLabel?.text = fruits[indexPath.row] return cell } 

}

希望有所帮助