0

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

-(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;

            }

        }

    }

}

它适用于所有可见的细胞。但我无法使用此代码访问任何不可见的单元格。单元格返回零。所以隐形单元格中的开关不会隐藏。我该如何克服这个问题。请帮我

还有一件事,动画在这里不起作用。但这是次要问题。

4

1 回答 1

1

修改了你的代码看看

-(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];
}
于 2013-03-25T09:28:42.487 回答