基本上我有一个uitableview,每个单元格都包含一个uiprogressview和一个uiswitch。切换开关可切换进度视图的可见性。
但
如果我在行中切换开关:0。行中的进度视图:0和行:11显示。
我的开关有这个: [(UISwitch *)cell.accessoryView addTarget:self action:@selector(PrefetchStudy :) forControlEvents:UIControlEventValueChanged];
这是行动:
-(void)PrefetchStudy:(id)sender
{
UISwitch *tmpSwitch = (UISwitch*)sender;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:tmpSwitch.tag inSection:0];
CustomCell *cell = (CustomCell *)[self.tableview cellForRowAtIndexPath:indexPath];
NSString *tmp = cell.patient_name.text;
NSLog(@"This is the patient name of the selected row: %@", tmp);
if(tmpSwitch.on)
{
cell.progressBar.hidden = NO;
}
else
{
cell.progressBar.hidden = YES;
}
}
有关为何发生这种情况的任何建议?
答案 0 :(得分:0)
您是否在代码中添加了一些断点?如果是这样,这个方法被调用两次(一次是第0行,第一次是第11行),还是cellForRowAtIndexPath方法错误地更新第11行,第0行的可见性?
答案 1 :(得分:0)
由于UITableView重用uitableviewcell以获得更好的性能,因此不能指望每个单元格都是唯一的。 (这种情况下第11行重用第1行单元格),您应该重置单元格的状态:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath