Mam TableCellViewController do zarządzania komórkami w moim UITableView. Każda komórka ma etykietę na UISwitch (dictTypeSwitch). Chcę przypisać metodę przełączania zdarzeń, aby móc zapisać stan przycisku.Dodaj niestandardowe działanie: @selector do UISwitch na TableViewCell
tej pory robiłem to:
przypisać funkcję setstate sprzeciwu:
[cell.dictTypeSwitch addTarget:self action:@selector(setState:) forControlEvents:UIControlEventTouchUpInside];
funkcję obsługi zdarzenia:
- (void)setState:(id)sender {
BOOL state = [sender isOn];
NSString *rez = state == YES ? @"YES" : @"NO";
NSLog(rez);
}
Od nadawcy uzyskać obiekt UISwitch z którego można uzyskać stan.
Do tej pory wszystko jest w porządku.
Ale jeśli chcę zapisać stan UISwitch, muszę uzyskać rowIndex dla tej komórki. Jak mogę to osiągnąć?
The cells are made inside function cellForRowAtIndexPath. Se the code bellow:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"DictionariesTableCellViewController";
DictionariesTableCellViewController *cell = (DictionariesTableCellViewController *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"DictionariesTableCellViewController" owner:nil options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (DictionariesTableCellViewController *) currentObject;
break;
}
}
}
// Configure the cell...
cell.dictTypeLabel.text = [NSString stringWithFormat:@"row - %i",indexPath.row];
[cell.dictTypeSwitch addTarget:self action:@selector(setState:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
Dzięki
Dzięki Kenny ... –