Watchkit: таблица с двумя шаблонами в динамических строках

Как создать два разных шаблона динамической строки в WKInterfaceTable? Только для одного шаблона я использую функции

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

Вопрос: Как получить 2 типа ряда?


person vitalii    schedule 27.11.2014    source источник


Ответы (3)


Вам нужен -[WKInterfaceTable setRowTypes:] :

[self.myTable setRowTypes:@[@"RowType1", @"RowType2"]];
MyRowType1Controller *row1 = [self.myTable rowControllerAtIndex:0];
MyRowType2Controller *row2 = [self.myTable rowControllerAtIndex:1];
person Dave DeLong    schedule 28.11.2014
comment
Спасибо, я не мог понять, что нам нужно вручную указывать массив типов для каждой строки раньше. Работает для меня! - person vitalii; 28.11.2014

Основываясь на ответе @dave-delong (правильный!), Большинство таблиц будут иметь разные типы строк, и массив должен это отражать. Например, для таблицы с заголовком, 4 строками информации и нижним колонтитулом потребуется массив, который выглядит примерно так:

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

Быстрое решение для случая с динамическим подсчетом ячеек:

    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]
        }
    }
}
person David Kyslenko    schedule 03.06.2019