如果我执行insertRowsAtIndexPaths,UITableView会闪烁

时间:2015-04-22 07:52:20

标签: ios uitableview

我正在使用UITableView方法向insertRowsAtIndexPaths添加行。我将此代码放在[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;
}

1 个答案:

答案 0 :(得分:1)

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

示例:

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
}

}

希望有所帮助