优化UITableview以处理大量数据

时间:2016-05-21 07:02:21

标签: ios objective-c uitableview

我正在使用核心数据来存储和检索我从Web服务获取的数据。 我在表格视图中显示这些数据。 以下是我的cellForRowAtIndexPath方法。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    StatutoryMappingCell * cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
    if (!cell)
    {
        [tableView registerNib:[UINib nibWithNibName:@"KnowledgeMainCell" bundle:nil] forCellReuseIdentifier:@"myCell"];
        cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
    }
    cell.statMapping.text = [self.items objectAtIndex:indexPath.row];

    return cell;
} 

self.items包含必须在tableview

中加载的数据

我面临的问题是,

  1. 我从服务器获取大量数据,就像2000条记录一样。所以tableview负载变得非常慢。如何优化上述代码来处理如此庞大的记录?
  2. 我有什么方法可以在iOS中进行分页,用户可以在底部点击加载更多按钮来检索更多内容,如在脸书中?

1 个答案:

答案 0 :(得分:1)

@MGR,使用NsfetchedresultsController可以实现它。

您可以在此处查询CD中的数据。

对于Swift:

lazy var fetchedResultsController: NSFetchedResultsController = {
    // Initialize Fetch Request
    let fetchRequest = NSFetchRequest(entityName: "Item")

    // Add Sort Descriptors
    let sortDescriptor = NSSortDescriptor(key: "createdAt", ascending: true)
    fetchRequest.sortDescriptors = [sortDescriptor]

    // Here you can set the limit of Fetch Using FetchRequest property 

    // Initialize Fetched Results Controller
    let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)

    // Configure Fetched Results Controller
    fetchedResultsController.delegate = self

    return fetchedResultsController
}()

目标c:

在.h文件中创建一个属性

@property(强,非原子)NSFetchedResultsController * fetchedResultsController;

.m中的

实现了这个

- (NSFetchedResultsController *)fetchedResultsController
{
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }


    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"EntityName" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    [fetchRequest setFetchBatchSize:20];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];

    NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    _fetchedResultsController = aFetchedResultsController;

    NSArray *result = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
    NSLog(@"Result: %@", result);

    NSError *error = nil;
    if (![_fetchedResultsController performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _fetchedResultsController;
}

当核心数据中保存了新数据时,它将在NFC代表Metods的帮助下自动反映在Tableview中。

// MARK: Fetched Results Controller Delegate Methods
func controllerWillChangeContent(controller: NSFetchedResultsController) {
    tableView.beginUpdates()
}

//在单元格中发生所有更改后将调用此方法。

func controllerDidChangeContent(controller: NSFetchedResultsController) {
    tableView.endUpdates()
}

//每当您删除或插入或移动单元格时,都会调用此方法

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
    switch (type) {
    case .Insert:
        if let indexPath = newIndexPath {
            tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        }
        break;
    case .Delete:
        if let indexPath = indexPath {
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        }
        break;
    case .Update:
        if let indexPath = indexPath {
            let cell = tableView.cellForRowAtIndexPath(indexPath) as! ToDoCell
            configureCell(cell, atIndexPath: indexPath)
        }
        break;
    case .Move:
        if let indexPath = indexPath {
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        }

        if let newIndexPath = newIndexPath {
            tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
        }
        break;
    }
}

多数民众赞成。希望这会对你有所帮助。

供参考,请点击此链接。https://www.hackingwithswift.com/read/38/10/optimizing-core-data-performance-using-nsfetchedresultscontrolle

相关问题