gpt4 book ai didi

ios - 无法访问uitableview中不可见的单元格

转载 作者:行者123 更新时间:2023-11-29 04:03:11 24 4
gpt4 key购买 nike

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

-(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)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];
}

关于ios - 无法访问uitableview中不可见的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15611303/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com