gpt4 book ai didi

ios - 如果我执行 insertRowsAtIndexPaths,UITableView 会闪烁

转载 作者:行者123 更新时间:2023-11-29 02:05:59 30 4
gpt4 key购买 nike

我正在使用 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;
}

最佳答案

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

示例:

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
}

希望对你有帮助

关于ios - 如果我执行 insertRowsAtIndexPaths,UITableView 会闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29791090/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com