Мой кодКак получить индекс WKInterfacebutton, используемый в WKinterfaceTable?
-(void)addSelectionTarget:(id)target action:(SEL)action { //You will need to add properties for these. self.selectionTarget = target; self.selectionAction = action; } //Call this when you want to call back to your interface controller - (void)fireSelectionAction { [self.selectionTarget performSelector:self.selectionAction]; //Or to do it without warnings on ARC IMP imp = [self.selectionTarget methodForSelector:self.selectionAction]; void (*func)(id, SEL) = (void *)imp; func(self.selectionTarget, self.selectionAction); } -(IBAction)btnclicked:(id)sender{ [self fireSelectionAction]; }
ответ
Создайте подкласс MyButton
вашей кнопки. MyButton
должно иметь имущество index
.
Во время создания вашей строки таблицы задайте указатель кнопки.
В вашем методе btnclicked
отливать кнопку MyButton
и прочитать индекс
Чтобы получить индексы резьбовых кнопок в WKInterfaceTable, вам нужно сделать несколько шагов:
кнопку 2.Every в ячейке (в раскадровке) должна есть REFERENCING Outlets в CustomTableCell.h
@property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceButton *firstButton;
@property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceButton *secondButton;
@property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceButton *thirdButton;
и настройка действия в CustomTableCell.m
- (IBAction)firstButtonTapped
{
}
- (IBAction)secondButtonTapped
{
}
- (IBAction)thirdButtonTapped
{
}
3.Add свойству индекса класса CustomTableCell (для проверки выбираемые строки) и передать имущество (показать информацию к InterfaceController о кнопке выбора в строке). Также создайте протокол для использования делегата. Весь код ниже должен быть в CustomTableCell.h.
@protocol TableCellButtonTappedProtocol <NSObject>
@optional
-(void)buttonTappedAtIndex:(NSInteger)index inRow:(NSInteger)row;
@end
@interface CustomTableCell : NSObject
@property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceButton *firstButton;
@property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceButton *secondButton;
@property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceButton *thirdButton;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic,weak) WKInterfaceController<TableCellButtonTappedProtocol> *delegate;
@end
4.Go в .m файл и добавить делегат вызов каждому методу buttonTapped с использованием индекса действия (1,2,3 - строка кнопка номер).
- (IBAction)firstButtonTapped
{
if ([self.delegate respondsToSelector:@selector(buttonTappedAtIndex:inRow:)])
{
[self.delegate buttonTappedAtIndex:1 inRow:self.index];
}
}
- (IBAction)secondButtonTapped
{
if ([self.delegate respondsToSelector:@selector(buttonTappedAtIndex:inRow:)])
{
[self.delegate buttonTappedAtIndex:2 inRow:self.index];
}
}
- (IBAction)thirdButtonTapped
{
if ([self.delegate respondsToSelector:@selector(buttonTappedAtIndex:inRow:)])
{
[self.delegate buttonTappedAtIndex:3 inRow:self.index];
}
}
5.Go к вашему InterfaceController - добавить протокол (TableCellButtonTappedProtocol) к классу InterfaceController (не забудьте сделать импорт CustomTableCell.h), чем перейти к методу Configure-таблицы, и вы можете инициализировать каждый грести с индексом, что вам нужно
for(NSInteger i = 0; i<self.table.numberOfRows;)
{
CustomTableCell *cell = [self.table rowControllerAtIndex:i];
cell.index = i;
cell.delegate = self;
[cell.firstButton setTitle:[NSString stringWithFormat:@"%d",1]];
[cell.secondButton setTitle:[NSString stringWithFormat:@"%d",2]];
[cell.thirdButton setTitle:[NSString stringWithFormat:@"%d",3]];
}
6.In ваш InterfaceController реализовать метод из buttonTappedAtIndex протокола: InRow:
-(void)buttonTappedAtIndex:(NSInteger)index inRow:(NSInteger)row
{
NSLog(@"index = %d; Row = %d",index,row);
}
Run т проект, нажмите на каждую кнопку на тренажере, ваш журнал должен быть
2016-09-23 11:56:44.989 watchKit Extension[92239:2117977] index = 1; Row = 0
2016-09-23 11:56:46.212 watchKit Extension[92239:2117977] index = 2; Row = 0
2016-09-23 11:56:47.244 watchKit Extension[92239:2117977] index = 3; Row = 0
2016-09-23 11:56:49.180 watchKit Extension[92239:2117977] index = 1; Row = 1
2016-09-23 11:56:50.708 watchKit Extension[92239:2117977] index = 2; Row = 1
2016-09-23 11:56:51.540 watchKit Extension[92239:2117977] index = 3; Row = 1
2016-09-23 11:56:54.340 watchKit Extension[92239:2117977] index = 1; Row = 2
2016-09-23 11:56:54.804 watchKit Extension[92239:2117977] index = 2; Row = 2
2016-09-23 11:56:55.212 watchKit Extension[92239:2117977] index = 3; Row = 2
- 1. Обработка действий WKInterfaceButton в WKInterfaceTable
- 2. Обработка событий WKInterfaceButton в WKInterfaceTable
- 3. WKInterfaceTable, WKInterfaceButton и методы действий
- 4. Как получить название от WKInterfaceButton
- 5. Как установить название WKInterfaceButton?
- 6. Как скрыть WKInterfaceButton в watchkit
- 7. Как создать разделы в WKInterfaceTable
- 8. Paginated WKInterfaceTable
- 9. Как программно установить шрифт WKInterfaceButton?
- 10. Индекс ListViewItem, используемый в Tapped Event
- 11. Выделите WKInterfaceTable в WatchKit?
- 12. WKInterfaceButton multiple line title
- 13. WKInterfaceTable, контроллеры строк и действия кнопок в Swift
- 14. Обнаруживать WKInterfaceButton непрерывно?
- 15. Время отклика WKInterfaceButton
- 16. Как сделать круглый WKInterfaceButton в watchkit?
- 17. Скрытый статус WKInterfaceButton в WatchKit
- 18. Как получить ресурс, используемый rethinkdb?
- 19. Выбор группы перехвата в WKInterfaceTable
- 20. Как получить Zend_Config, используемый Zend_Application?
- 21. Как получить наименее используемый товар?
- 22. Найти максимальный z-индекс, используемый на сайте
- 23. индекс, используемый после функции Split (jquery)?
- 24. Удалить ячейку от WKInterfaceTable
- 25. Как реализовать scrollToRowAtIndex: анимированные: метод в WKInterfaceTable
- 26. WKInterfaceTable как идентифицировать кнопку в каждой строке?
- 27. Как повторно использовать WKInterfaceTable в WatchKit
- 28. Как уменьшить расстояние между строками в WKInterfaceTable
- 29. Multiple WKInterfaceLabel на WKInterfaceTable
- 30. WKInterfaceTable ширина полосы прокрутки