在查看我们的源代码时附加 ?ts=2,这将使您的浏览器停止错误地表示制表符。
该项目在很大程度上借鉴了 IGInterfaceDataTable,但一开始就专注于支持 NSFetchedResultsController、可测试性和支持 Apple 推荐但未提供帮助的位(批量加载)。
ROInterfaceTable 为 WKInterfaceTable 添加了对分区的支持。它支持与您在 UITableView 中熟悉的相同的批量更新,使其与 NSFetchedResultsController
的更改通知兼容。这用于Downcast 的 Apple Watch 应用程序,目前处于测试阶段。
beginUpdates
和 endUpdates
批量应用更改NSFetchedResultsControllerDelegate
兼容遗憾的是,WKInterfaceTable 默认只支持扁平数据源,这对于从 iOS 过来的人来说可能有点痛苦。ROInterfaceTable 添加了您熟悉的基于索引路径的方法,您将使用与 UITableView 一样的方式添加、移动和删除行和分区。
只能按增量方式将行添加到 WKInterfaceTable。ROInterfaceTable 添加了熟悉的 beginUpdates
和 endUpdates
方法,允许将更改分组在一起,并按正确的顺序应用,无论它们的顺序如何。
NSFetchedResultsController 报告移动操作顺序,这阻止我们立即对表格应用更改。批量更新支持使得可以像使用 UITableView 一样在 ROInterfaceTable 中使用此类。
将过多行加载到WKInterfaceTable中可能会导致内存消耗问题、加载时间延迟和崩溃。苹果建议一次只加载预定数量的行,然后提供一种方式让用户请求更多行。通过允许您指定批量大小,ROInterfaceTable使得这个过程变得简单。
如果您指定了每批次的延迟,ROInterfaceTable也会自动加载所有批次。这将导致它先加载第一批次,等待一会儿,然后加载下一批次,等待一会儿,如此循环,直到将所有行添加到表中。
CocoaPods
pod 'ROInterfaceTable'
#import <ROInterfaceTable/ROInterfaceTable.h>
或者,将以下文件复制到您的项目中
WKInterfaceTable+ROInterfaceTable.h|m
ROInterfaceTableBackingStore.h|m
ROInterfaceTableProtocol.h
ROInterfaceTableDataStore.h
ROInterfaceTableDelegate.h
ROInterfaceTable.h (umbrella header)
// Importing
#import "ROInterfaceTable.h"
首先,实现ROInterfaceTableDataSource
协议。只需实现两个方法即可开始使用
-(NSInteger)numberOfRowsInTable:(id)table section:(NSInteger)section {
return self.fetchedResultsController.fetchedObjects.count;
}
-(NSString *)table:(id)table identifierForRowAtIndexPath:(NSIndexPath *)indexPath {
return @"RowIdentifier"; // Make sure this is set in your storyboard!
}
分区标题和页脚
-(NSInteger)numberOfSectionsInTable {
return self.fetchedResultsController.sections.count;
}
-(NSInteger)numberOfRowsInTable:(id)table section:(NSInteger)section {
return [][self.fetchedResultsController.sections[section] objects] count];
}
-(NSString *)table:(id)table identifierForSectionHeader:(NSInteger)section {
return @"SectionHeaderRowController";
}
-(NSString *)table:(id)table identifierForRowAtIndexPath:(NSIndexPath *)indexPath {
return @"RowIdentifier";
}
-(NSString *)table:(id)table identifierForSectionFooter:(NSInteger)section {
return @"SectionFooterRowController";
}
批量加载您需要实现ROInterfaceTableDelegate,并设置self.table.ro_delegate以便调用-[WKInterfaceTable table:loadedRowBatch:rowsShown:hasMoreRows:]
方法。
-(NSUInteger)numberOfRowsPerBatchInTable:(id)table {
return 5; // Load 5 'normal' rows per batch. Headers & footers don't count
}
-(void)table:(id)table loadedRowBatch:(NSUInteger)batch rowsShown:(NSUInteger)rowsShown hasMoreRows:(BOOL)hasMoreRows {
// Assuming loadNextBatchButton is a button you've added below the
// WKInterfaceTable in your storyboard.
//
// Hide the button if no more rows are available to load
[self.loadNextBatchButton setHidden:!hasMoreRows];
if (hasMoreRows) {
// Update the button's title to read 'Show Next X'
[self.loadNextBatchButton setTitle:[NSString stringWithFormat:@"Show Next %@",
@(MIN(self.fetchedResultsController.fetchedObjects.count - rowsShown,
[self numberOfRowsPerBatchInTable:table]))]];
}
}
自动批量加载
-(NSUInteger)numberOfRowsPerBatchInTable:(id)table {
return 5; // Load 5 'normal' rows per batch. Headers & footers don't count
}
-(NSTimeInterval)autoBatchLoadingDelayInTable:(id)table {
return 3.0; // Load a batch every 3 seconds
}