8

如何在 WKInterfaceTable 中实例化两个不同的动态行模板?我只对一个模板使用函数

 [self.stocksTable setNumberOfRows: self.stocksData.count withRowType:@"TableRow"];
 TableRow *row = [self.stocksTable rowControllerAtIndex:i];

问题:如何有两种类型的行?

4

3 回答 3

12

你想要-[WKInterfaceTable setRowTypes:]

[self.myTable setRowTypes:@[@"RowType1", @"RowType2"]];
MyRowType1Controller *row1 = [self.myTable rowControllerAtIndex:0];
MyRowType2Controller *row2 = [self.myTable rowControllerAtIndex:1];
于 2014-11-28T02:09:47.357 回答
0

基于@dave-delong(正确!)的答案,大多数表将混合行类型,并且数组必须反映这一点。例如,一个带有页眉、4 行信息和一个页脚的表格需要一个看起来像这样的数组:

NSArray *rowTypes = @[@"headerRowType", @"infoRowType", @"infoRowType", @"infoRowType", @"infoRowType", @"footerRowType"];
[self.myTable setRowTypes:rowTypes];
于 2015-11-06T02:28:06.093 回答
0

快速解决方案,用于动态细胞计数的情况:

    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]
        }
    }
}
于 2019-06-03T14:15:53.453 回答