【发布时间】:2017-03-09 06:40:21
【问题描述】:
我想根据UISwitch 的开/关状态显示/隐藏UIButton。滚动 tableview UIButton 时会更改状态。它也显示在其他单元格上。请指教。
在UITableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
EnhancedChecklistTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if ( cell == nil )
{
NSArray *topLevelObjects;
if(IS_IPAD)
{
topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"EnhancedChecklistTableViewCell" owner:self options:nil];
}
else
{
topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"EnhancedChecklistTableViewCell_Iphone" owner:self options:nil];
}
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[EnhancedChecklistTableViewCell class]])
{
cell = (EnhancedChecklistTableViewCell *)currentObject;
cell.delegate = self;
break;
}
}
}
R5OPERATORACTCHECKLISTS *inspectionTasks = [_data objectAtIndex:indexPath.section];
return cell;
}
所有开关都是使用自定义单元格中的 xib 文件添加的。我使用了一种在值更改事件后触发的方法。并根据我正在显示按钮的开关状态。但是在滚动时它会打扰。
【问题讨论】:
-
您能否分享一些您尝试过的代码,以便我们提供帮助。
-
细胞被重复使用;您不能依赖单元格本身的开关状态;您需要将开关状态存储在一些数据结构中,例如数组,并在
cellForRowAt:中使用它来重置单元状态 -
请出示您的代码。 @斯瓦蒂
-
我使用了自定义单元格。并添加了来自 xib 文件的开关。开关的值更改事件,例如 if (sender == _buttonFollowUp ) { self.followUp = !_followUp; self.btnSave.hidden = 否; } else if (sender == _buttonFinalOccurence ) { self.finalOccurence = !_finalOccurence; self.btnSave.hidden = 否; } 像这样添加 .. 但滚动 tableview 后 .. 保存按钮将出现在所有单元格上。
-
你能告诉我你的
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {func 吗?我会帮你。 :)
标签: ios objective-c iphone ipad