0

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

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

打开时,单元格将添加到该部分的底部。但是当关闭时,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];


}

我查看了几乎所有关于 Stack 上的 deleteRowsAtIndexPaths 和 insertRowsAtIndexPaths 的帖子,但没有一个有大量示例可以处理/解决。

4

1 回答 1

2

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

[array addObjectsFromArray:indexPaths];

我想你的意思是:

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

如果不是这样,那么如果您可以明确说明预期的行为是什么,那将有所帮助。

于 2013-01-28T15:53:21.703 回答