我有一个表视图,每个单元格中都有一个开关。我想在编辑模式下隐藏开关。我完成了以下代码
-(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;
}
}
}
}
它适用于所有可见细胞。但我无法使用此代码访问任何不可见的单元格。单元格返回零。所以不可见单元格中的开关不会隐藏。我怎样才能克服这个问题。请帮帮我
还有一件事,动画不在这里工作。但这是次要问题。
答案 0 :(得分: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];
}