表编辑完成后,UISwitch在UITableViewCell中仍然可以略微显示
问题描述:
在关闭表编辑模式后,我仍然有一个editingAccessoryView
的editingAccessoryView
问题。表编辑完成后,UISwitch在UITableViewCell中仍然可以略微显示
当前我正在使用UISwitch
作为编辑配件视图,并让表视图句柄在按下导航栏的编辑按钮时通过动画移动编辑视图的开启/关闭屏幕。
几乎所有的编辑配件都可以在屏幕外正确显示动画,但总是有两个不能使其脱离屏幕,然后在单元出列并重新使用时重用它们,以便在滚动过程中显示它们。
有没有其他人看到这个或有什么我在这里失踪?
我设立这样的细胞:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
AllStatCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AllStatCell"];
if (cell.editingAccessoryView == nil) {
UISwitch *statSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[statSwitch addTarget:self action:@selector(saveSwitchState:) forControlEvents:UIControlEventValueChanged];
cell.editingAccessoryView = statSwitch;
}
NSString *statName = [self statNameForIndexPath:indexPath];
cell.statName.text = statName;
[(UISwitch *)cell.editingAccessoryView setOn:[self switchValueForIndexPath:indexPath withStatNamed:statName]];
if (tableView.editing) cell.statValue.hidden = YES;
return cell;
}
我试图重写setEditing
方法的延迟之后重新加载表数据以允许动画来完成,但它仅适用有时候
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
// Insert/delete the stat rows depending on editing mode
[self rebuildTableDataAnimated:YES];
double delayInSeconds = 0.5;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// Trying to fix bug that shows part of a UISwitch on screen still even though editing mode is completed
[self.tableView reloadData];
});
}
这里有一个截屏:
答
这感觉就像这样一个黑客,所以希望别人有一个更好的解决办法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
AllStatCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AllStatCell"];
NSString *statName = [self statNameForIndexPath:indexPath];
// Hack fix for a bug that shows part of a UISwitch on screen still even though editing mode is completed
// Configure stat on/off switches
cell.editingAccessoryView = [self cellEditingAccessoryViewForEditing:tableView.editing];
if (tableView.editing) [(UISwitch *)cell.editingAccessoryView setOn:[self switchValueForIndexPath:indexPath withStatNamed:statName]];
cell.statName.text = statName;
[cell.statValue setHidden:tableView.editing];
return cell;
}
// Hack fix for a bug that shows part of a UISwitch on screen still even though editing mode is completed
- (UISwitch *)cellEditingAccessoryViewForEditing:(BOOL)tableIsEditing
{
if (tableIsEditing) {
UISwitch *statSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[statSwitch addTarget:self action:@selector(saveSwitchState:) forControlEvents:UIControlEventValueChanged];
return statSwitch;
}
return nil;
}
答
旧线,但有UIStepper其在Xcode 7同样的问题。我的解决方案是将其嵌入到左侧和右侧有一些填充的UIView中,然后将父视图设置为editingAccessoryView。它的工作原理,并不是一个破解。
我的第一次尝试是在执行编辑之后重新加载表格。 – 2013-03-02 14:47:22
@flexaddicted不幸的是,没有工作 – iwasrobbed 2013-03-02 15:19:04
对不起,我没有看到它。如何重新加载它像'[tableView setEditing:NO animated:YES]; [tableView reloadData];'?你在后台执行一些计算吗? – 2013-03-02 15:28:45