如何在激活UISwitch时添加/删除UITableViewCell?

时间:2013-01-28 12:29:33

标签: ios uitableview nsmutablearray uiswitch

以下是视频显示的问题: http://www.youtube.com/watch?v=vWznBUsppRg

在向一个部分添加单元格时,我的Array会出现奇怪的行为。我正在开发一个mobilesubstrate tweak ..这就是为什么我没有发布整个方法。

打开时,Cell将添加到该部分的底部。但是当关闭时,UISwitch单元格下的单元格会像预期的那样被移除,但会搞砸indexPaths。

.m的顶部:

static NSUserDefaults *prefs = nil;
static UISwitch *switchView = nil;
static NSMutableArray *array = nil;

-viewDidLoad我正在使用 -

BOOL state = [prefs boolForKey:@"betaVid"];

array = [[NSMutableArray alloc] initWithObjects:@"Show Cached Videos", @"(Beta) Send Videos", @"Video Quality", @"Visit Website", @"Made by: @CokePokes", nil];

if (state == FALSE){
    [array removeObject:@"Video Quality"]; //remove from array

}

-numberOfRowsInSection我有:

if (section == 3){ 

    return [array count];

}

return section;

-cellForRowAtIndexPath我有:

prefs = [NSUserDefaults standardUserDefaults];
BOOL state = [prefs boolForKey:@"betaVid"];

if (indexPath.section == 3){

static NSString *CellIdentifier = @"Cell";
//NSString *CellIdentifier = [NSString  stringWithFormat:@"Cell_%d",indexPath.row];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

}

// Configure the cell...

    cell.textLabel.text = [NSString stringWithFormat:@"%@", [array objectAtIndex:indexPath.row]];

    NSInteger counted = [array count];
    NSLog(@"~~~~~~~~~~~~~~~~~~~hmmmmm.... Really is: %d", counted);

    if (counted == 4){

        NSLog(@"~~~~~~~~~~~~~~~~~~~Array count is _4_. Really is: %d", counted);


        if (indexPath.row == 0){

            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.textLabel.textAlignment = UITextAlignmentLeft;

        } else if (indexPath.row == 1){

            switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
            cell.accessoryView = switchView;
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

            switchView.on = [prefs boolForKey:@"betaVid"];

            [switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];

        } else if (indexPath.row == 2){


          //set up cell appearance..not important now


        } else if (indexPath.row == 3){

           //set up cell appearance..not important now

        }

    } else if (counted == 5){
        NSLog(@"~~~~~~~~~~~~~~~~~~~~Array count is _5_. Really is: %d", counted);

        if (indexPath.row == 0){

            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.textLabel.textAlignment = UITextAlignmentLeft;

        } else if (indexPath.row == 1){

            switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
            cell.accessoryView = switchView;
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

            switchView.on = [prefs boolForKey:@"betaVid"];

            [switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];

        } else if (indexPath.row == 2){

            //set up cell appearance..not important now

        }

return cell;

}

return indexPath;

最后UISwitch switchChanged:

switchView = sender;
NSLog( @"The switch is %@", switchView.on ? @"ON" : @"OFF" );
prefs = [NSUserDefaults standardUserDefaults];

[prefs setBool:switchView.on forKey:@"betaVid"];
[prefs synchronize];

BOOL state = [prefs boolForKey:@"betaVid"];

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:2 inSection:3];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];

if (state == TRUE) {        

    [self.table beginUpdates];
    [array addObjectsFromArray:indexPaths];
    [self.table insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    [self.table endUpdates];

    [self.table reloadData];

} else if (state == FALSE){

    [self.table beginUpdates];
    [array removeObjectAtIndex:indexPath.row];
    [self.table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    [self.table endUpdates];

   [self.table reloadData];


}

我几乎查看了有关deleteRowsAtIndexPaths&的所有帖子。 Stack上的insertRowsAtIndexPaths但没有一个可以在/周围工作的大量示例。

1 个答案:

答案 0 :(得分:2)

当开关打开时,您的数据模型(阵列)被以下行损坏:

[array addObjectsFromArray:indexPaths];

我认为你的意思是:

[array insertObject:@"Video Quality" atIndex:indexPath.row];

如果不是这样,那么如果你能明确说明预期的行为是什么就会有所帮助。