gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:40:51 24 4
gpt4 key购买 nike

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

将单元格添加到部分时,我的数组出现奇怪的行为。我正在开发 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 的所有帖子,但没有一个有大量的示例可以处理/解决。

最佳答案

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

[array addObjectsFromArray:indexPaths];

我想你的意思是:

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

如果不是这样,如果您能明确说明预期行为是什么,将会有所帮助。

关于ios - 如何在激活 UISwitch 时添加/删除 UITableViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14562075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com