如果我执行 insertRowsAtIndexPaths,UITableView 会闪烁 [英] UITableView blinks if I do insertRowsAtIndexPaths

查看:32
本文介绍了如果我执行 insertRowsAtIndexPaths,UITableView 会闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 insertRowsAtIndexPaths 方法向 UITableView 添加行.我把这段代码放在 [myTable beginUpdates];[myTable endUpdates]; 之间.问题是我的表格闪烁然后滚动到最后一行?

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 ?

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

What might be the problem ? If I comment [myTable beginUpdates]; and [myTable endUpdates];: then it works fine.

这是我的代码:

#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;
}

推荐答案

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

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

示例:

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
}

}

希望有帮助

这篇关于如果我执行 insertRowsAtIndexPaths,UITableView 会闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆