用UISwitch显示和隐藏UITableViewCell太快崩溃 [英] Showing and hiding UITableViewCell with UISwitch too fast crashes

查看:258
本文介绍了用UISwitch显示和隐藏UITableViewCell太快崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableView,向用户提供一些设置。一些单元格被隐藏,除非UISwitch处于开位置。我有以下代码:

   - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
return switchPush.on? 6:1;
}

//钩住switchPush的'Value Changed'动作
- (IBAction)togglePush:(id)sender {
NSMutableArray * indexPaths = NSMutableArray arrayWithCapacity:0];
for(int i = 1; i <= 5; i ++){
[indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}

[tableView beginUpdates];
if(switchPush.on){
[tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
} else {
[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
}
[tableView endUpdates];
}

这样工作正常,直到我快速连续两次切换UISwitch双击),在这种情况下应用程序会崩溃与

 无效的表视图更新。应用程序已请求更新表视图,该更新与数据源提供的状态不一致。 

我知道它是由于错误的返回值numberOfRowsInSection,位置,而单元格动画仍在播放。我试过禁用切换和挂钩代码在其他事件处理程序,但似乎没有防止崩溃。使用reloadData而不是动画也解决了问题,但我更喜欢好的动画。



有没有人知道正确的实现方式?

$ b $另一个(更优雅的)问题解决方案是:



我修改了Alan MacGregor - (IBAction)SwitchDidChange:(id)sender 方法:

   - (IBAction)SwitchDidChange:(UISwitch *)source {
if(_showRows!= source.on){
NSArray * aryTemp = [[NSArray alloc] initWithObjects:
[NSIndexPath indexPathForRow :1 inSection:0],
[NSIndexPath indexPathForRow:2 inSection:0],
[NSIndexPath indexPathForRow:3 inSection:0] ];
[_tblView beginUpdates];
_showRows = source.on;
if(_showRows){
[_tblView insertSections:aryTemp withRowAnimation:UITableViewRowAnimationFade];
}
else {
[_tblView deleteSections:aryTemp withRowAnimation:UITableViewRowAnimationFade];
}
[_tblView endUpdates];
}
}

其他部分保持不变。


I've got a UITableView that presents some settings to the user. Some cells are hidden unless a UISwitch is in the 'On' position. I've got the following code:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return switchPush.on ? 6 : 1;
}

// Hooked to the 'Value Changed' action of the switchPush
- (IBAction)togglePush:(id)sender {
    NSMutableArray *indexPaths = [NSMutableArray arrayWithCapacity:0];
    for(int i = 1; i <= 5; i++) {
        [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
    }

   [tableView beginUpdates];
    if(switchPush.on) {
        [tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
    } else {
        [tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
    }
   [tableView endUpdates];
}

This works as expected, until I switch the UISwitch twice in rapid succession (by double tapping), in which case the application crashes with a

Invalid table view update. The application has requested an update to the table view that is inconsistent with the state provided by the data source.

I know that it is caused by the wrong return value of numberOfRowsInSection as the switch is back in its original position while the cell animation is still playing. I've tried disabling the toggle and hooking the code under other event handlers but nothing seems to prevent the crash. Using reloadData instead of the animation also solves to problem but I would prefer the nice animations.

Does anybody know a correct way of implementing this?

解决方案

Another (more elegant) solution at the problem is this:

I modified the Alan MacGregor - (IBAction)SwitchDidChange:(id)sender method in this way:

- (IBAction)SwitchDidChange:(UISwitch *)source {
     if (_showRows != source.on) {
         NSArray *aryTemp = [[NSArray alloc] initWithObjects:
                    [NSIndexPath indexPathForRow:1 inSection:0],
                    [NSIndexPath indexPathForRow:2 inSection:0],
                    [NSIndexPath indexPathForRow:3 inSection:0],
                    [NSIndexPath indexPathForRow:4 inSection:0],nil];
         [_tblView beginUpdates];
         _showRows = source.on;
         if (_showRows) {
             [_tblView insertSections:aryTemp withRowAnimation:UITableViewRowAnimationFade];
         }
         else {
             [_tblView deleteSections:aryTemp withRowAnimation:UITableViewRowAnimationFade];
         }
         [_tblView endUpdates];
     }
}

The other parts stay unchanged.

这篇关于用UISwitch显示和隐藏UITableViewCell太快崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆