Question

I am trying to get the row number when the switch is toggled inside the uitableviewcell however i am getting null for the indexpath under the enableAbsense method. Any idea where went wrong? Here are the codes. Thanks in advance.

in the implementation file: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

if (tableView == DetailsViewClassTable) {

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];// forIndexPath:indexPath];

    ClassRecord * ClassObject;
    ClassObject = [DetailsViewClassArray objectAtIndex:indexPath.row];

    //add switch button switch
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        switchAbsense = [[UISwitch alloc] init];
        CGSize switchSize = [switchAbsense sizeThatFits:CGSizeZero];
        switchAbsense.frame = CGRectMake(cell.contentView.bounds.size.width - switchSize.width - 355.0f,
                             (cell.contentView.bounds.size.height - switchSize.height) / 5.0f,
                             switchSize.width,
                             switchSize.height);
        switchAbsense.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
        switchAbsense.tag = 101;
        [switchAbsense addTarget:self action:@selector(enableAbsense:) forControlEvents:UIControlEventValueChanged];
        [cell.contentView addSubview:switchAbsense];



    }else{
        switchAbsense=(UISwitch *)[cell.contentView viewWithTag:101];

    }


    // Configure the cell...
    if ([ClassObject.absense isEqual:@"Yes"]){
        switchAbsense.on=YES;
    }else{
        switchAbsense.on=NO;
    }

    if ([ClassObject.makeup isEqual:@"Yes"]){
        switchMakeup.on=YES;
    }else{
        switchMakeup.on=NO;
    }



    //Accessory
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;

}

}

-(void) enableAbsense:(UISwitch *)sender {

UITableViewCell *cell = (UITableViewCell *)sender.superview.superview;
NSIndexPath *indexpath =[DetailsViewClassTable indexPathForCell:cell];
NSLog(@"%ld",(long)indexpath.row);

}

in the header file @property (strong, nonatomic) IBOutlet UITableView *DetailsViewClassTable;

Was it helpful?

Solution

-(void) enableAbsense:(UISwitch *)sender
{

    CGPoint point = [sender.superview convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top