Watchkit:动态行中包含两个模板的表

时间:2014-11-27 19:12:52

标签: ios watchkit apple-watch wkinterfacetable

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

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

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

3 个答案:

答案 0 :(得分:10)

您想要-[WKInterfaceTable setRowTypes:]

[self.myTable setRowTypes:@[@"RowType1", @"RowType2"]];
MyRowType1Controller *row1 = [self.myTable rowControllerAtIndex:0];
MyRowType2Controller *row2 = [self.myTable rowControllerAtIndex:1];

答案 1 :(得分:0)

在@ dave-delong' s(正确!)答案的基础上,大多数表格将混合使用行类型,并且数组必须反映这一点。例如,带有标题,4行信息和页脚的表格需要一个如下所示的数组:

NSArray *rowTypes = @[@"headerRowType", @"infoRowType", @"infoRowType", @"infoRowType", @"infoRowType", @"footerRowType"];
[self.myTable setRowTypes:rowTypes];

答案 2 :(得分: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]
        }
    }
}