问题
I am adding rows to UITableView
using the insertRowsAtIndexPaths
method. I put this code between [myTable beginUpdates];
and [myTable endUpdates];
. The problem is that my table is blinking and then scrolls to the last row ?
What might be the problem ? If I comment [myTable beginUpdates];
and [myTable endUpdates];
: then it works fine.
Here is my code:
#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;
}
回答1:
I don't understand the question very well, if the question is how to insert/delete cells of table view, we must remember : The number of rows contained in an existing section after the update must be equal to the number of rows contained in that section before the update, plus or minus the number of rows inserted or deleted from that section
Example :
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
}
}
Hope that helps
来源:https://stackoverflow.com/questions/29791090/uitableview-blinks-if-i-do-insertrowsatindexpaths