如何在 WKInterfaceTable 中实例化两个不同的动态行模板?我只对一个模板使用函数
[self.stocksTable setNumberOfRows: self.stocksData.count withRowType:@"TableRow"];
TableRow *row = [self.stocksTable rowControllerAtIndex:i];
问题:如何有两种类型的行?
如何在 WKInterfaceTable 中实例化两个不同的动态行模板?我只对一个模板使用函数
[self.stocksTable setNumberOfRows: self.stocksData.count withRowType:@"TableRow"];
TableRow *row = [self.stocksTable rowControllerAtIndex:i];
问题:如何有两种类型的行?
你想要-[WKInterfaceTable setRowTypes:]
:
[self.myTable setRowTypes:@[@"RowType1", @"RowType2"]];
MyRowType1Controller *row1 = [self.myTable rowControllerAtIndex:0];
MyRowType2Controller *row2 = [self.myTable rowControllerAtIndex:1];
基于@dave-delong(正确!)的答案,大多数表将混合行类型,并且数组必须反映这一点。例如,一个带有页眉、4 行信息和一个页脚的表格需要一个看起来像这样的数组:
NSArray *rowTypes = @[@"headerRowType", @"infoRowType", @"infoRowType", @"infoRowType", @"infoRowType", @"footerRowType"];
[self.myTable setRowTypes:rowTypes];
快速解决方案,用于动态细胞计数的情况:
let notificationRowTypes = Array(repeating: "notificationRow", count: watchNotifications.count)
let notificationDateRowTypes = Array(repeating: "notificationDateRow", count: watchNotifications.count)
let rowTypes = mergeArrays(notificationDateRowTypes, notificationRowTypes)
noficationsTable.setRowTypes(rowTypes)
updateTableRowsContent()
func mergeArrays<T>(_ arrays:[T] ...) -> [T] {
return (0..<arrays.map{$0.count}.max()!)
.flatMap{i in arrays.filter{i<$0.count}.map{$0[i]} }
}
func updateTableRowsContent() {
let numberOfRows = noficationsTable.numberOfRows
for index in 0..<numberOfRows {
switch index % 2 == 0 {
case true:
guard let controller = noficationsTable.rowController(at: index) as? NotificationDateRowController else { continue }
controller.notification = watchNotifications[index / 2]
case false:
guard let controller = noficationsTable.rowController(at: index) as? NotificationRowController else { continue }
controller.notification = watchNotifications[index / 2]
}
}
}