Не получать indexPath для нажатия UISwitch в UITableViewCell

Я добавил UISwitch в UITableViewCell, содержимое таблицы является динамическим, что означает, что в представлении таблицы может быть много UISwitch, мне нужно получить состояние UISwitch для каждого UITableViewCell, но не получать метод indexPath в accessoryButtonTappedForRowWithIndexPath.

мой код:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LocationCell *cell = (LocationCell *)[tableView 
                                          dequeueReusableCellWithIdentifier:@"LocationCell"];

    UISwitch *useLocationSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
    [cell addSubview:useLocationSwitch];
    cell.accessoryView = useLocationSwitch;

    [useLocationSwitch addTarget: self
               action: @selector(accessoryButtonTapped:withEvent:)
     forControlEvents: UIControlEventTouchUpInside];

    return cell;
}

- (void) accessoryButtonTapped: (UIControl *) button withEvent: (UIEvent *) event
{
    NSIndexPath * indexPath = [showLocationTableView indexPathForRowAtPoint: [[[event touchesForView: button] anyObject] locationInView: showLocationTableView]];
    if ( indexPath == nil )
        return;

    [showLocationTableView.delegate tableView: showLocationTableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"index path: %@", indexPath.row);
}

person Firdous    schedule 04.04.2012    source источник


Ответы (1)


Событие управления должно быть UIControlEventValueChanged.

Не UIControlEventTouchUpInside. Измените это и попробуйте еще раз.

Таким образом, оператор настройки действия должен быть следующим:

[useLocationSwitch addTarget: self
                      action: @selector(accessoryButtonTapped:withEvent:)
            forControlEvents: UIControlEventValueChanged];

Изменить:

- (void) accessoryButtonTapped: (UIControl *) button withEvent: (UIEvent *) event
{
    UISwitch *switch1 = (UISwitch *)button;
    UITableViewCell *cell = (UITableViewCell *)switch1.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    //NSIndexPath * indexPath = [showLocationTableView indexPathForRowAtPoint: [[[event touchesForView: button] anyObject] locationInView: showLocationTableView]];
    if ( indexPath == nil )
        return;

    [showLocationTableView.delegate tableView: showLocationTableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}
person Ilanchezhian    schedule 04.04.2012
comment
все еще не получает значение индекса - person Firdous; 04.04.2012
comment
Я обновил свой ответ. Его не тестировали. Я проверю это в ближайшее время (несколько минут), как только попаду в машину Mac. - person Ilanchezhian; 04.04.2012
comment
Да, он протестирован и должен дать ожидаемое значение. - person Ilanchezhian; 04.04.2012
comment
Используйте это в iOS 7 CGPoint switchPositionPoint = [sender convertPoint:CGPointZero toView:[self tableView]]; NSIndexPath *indexPath = [[self tableView] indexPathForRowAtPoint:switchPositionPoint]; - person user1872384; 28.03.2016