【问题标题】:cannot get the row number when the uiswitch is toggled within uitableviewcell在 uitableviewcell 中切换 uiswitch 时无法获取行号
【发布时间】:2014-06-18 16:24:58
【问题描述】:

当在 uitableviewcell 内切换开关时,我试图获取行号,但是在 enableAbsense 方法下,索引路径为空。知道哪里出了问题吗?这是代码。提前致谢。

在实现文件中: - (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);

}

在头文件中 @property (strong, nonatomic) IBOutlet UITableView *DetailsViewClassTable;

【问题讨论】:

    标签: uitableview uiswitch


    【解决方案1】:
    -(void) enableAbsense:(UISwitch *)sender
    {
    
        CGPoint point = [sender.superview convertPoint:CGPointZero toView:self.tableView];
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
    
    }
    

    【讨论】:

    • @user3477999:如果对您有用,请采纳。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多