Ios can';无法访问uitableview中的不可见单元格

Ios can';无法访问uitableview中的不可见单元格,ios,uitableview,Ios,Uitableview,我有一个表视图,每个单元格中都有一个开关。我想在编辑模式下隐藏开关。我完成了以下代码 -(void)displaySwitch:(BOOL)status { int count = [self.tblView numberOfRowsInSection:0]; int i; for (i = 0; i<count; i++) { UITableViewCell *eachCell = [self.tblView cellForRowAtInd

我有一个表视图,每个单元格中都有一个开关。我想在编辑模式下隐藏开关。我完成了以下代码

-(void)displaySwitch:(BOOL)status   {

    int count  = [self.tblView numberOfRowsInSection:0];
    int i;
    for (i = 0; i<count; i++) {
        UITableViewCell *eachCell = [self.tblView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        //NSLog(@"%@",eachCell);
        NSArray *subViews = [eachCell.contentView subviews];
        for (UISwitch *eachObject in subViews) {
            if ([eachObject isKindOfClass:[UISwitch class]]) {

                CATransition *animation = [CATransition animation];
                animation.type = kCATransitionFade;
                animation.subtype = kCATransitionFromLeft;
                animation.duration = 1.4;
                animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
                [eachObject.layer addAnimation:animation forKey:kCATransition];
                eachObject.hidden = status;

            }

        }

    }

}
-(void)显示开关:(BOOL)状态{
int count=[self.tblView numberOfRowsInSection:0];
int i;

对于(i=0;i修改的代码,请看一看

-(void)displaySwitch:(BOOL)status onCell:(UITableViewCell*)cell   {

    NSArray *subViews = [cell.contentView subviews];
    for (UISwitch *eachObject in subViews) {
        if ([eachObject isKindOfClass:[UISwitch class]]) {

            CATransition *animation = [CATransition animation];
            animation.type = kCATransitionFade;
            animation.subtype = kCATransitionFromLeft;
            animation.duration = 1.4;
            animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
            [eachObject.layer addAnimation:animation forKey:kCATransition];
            eachObject.hidden = status;

        }
   }
}
然后调用这个函数

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

    if(cell == nil){

        //Cell initialization

    }

    [self displaySwitch:self.editing onCell:cell];
}

当然,您无法访问它们,因为它们不是创建的。您需要将代码放在CellForRowatineXpath()中以实现您的要求。