0

我在一个表中有 5 个单独的部分,其中 2 个部分具有可自定义的单元格,当处于 EDITING 模式时,其中 2 个部分会生成一个额外的单元格,该单元格会将一个新单元格添加到列表中。我的问题是,当我调用编辑模式时,出现UISwitch故障!并决定跳来跳去,非常简单的代码,只是不知道为什么 UISwitch 在不应该跳转到不同部分的不同单元格时!

static NSInteger kCustomTextTag = 1;
static NSInteger kServiceTag = 3;
static NSInteger kConnectTag = 4;
static NSInteger kVibrateTag = 1;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{    
    Profile_ManagerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        cell.hidesAccessoryWhenEditing = YES;
    }

    UISwitch *swService = nil;
    swService = [[[UISwitch alloc] initWithFrame:CGRectMake(195, 8, 160, 27)] autorelease];
    [swService addTarget:self action:@selector(serviceSwAction:) forControlEvents:UIControlEventValueChanged];
    swService.tag = kServiceTag;

    UISwitch *swConnect = nil;
    swConnect = [[[UISwitch alloc] initWithFrame:CGRectMake(195, 8, 160, 27)] autorelease];
    [swConnect addTarget:self action:@selector(connectSwAction:) forControlEvents:UIControlEventValueChanged];
    swConnect.tag = kConnectTag;

    UISwitch *swVibrate = nil;
    swVibrate = [[[UISwitch alloc] initWithFrame:CGRectMake(195, 8, 160, 27)] autorelease];
    [swVibrate addTarget:self action:@selector(vibrateSwAction:) forControlEvents:UIControlEventValueChanged];
    swVibrate.tag = kVibrateTag;

    if(indexPath.section == 0)
    {
        //CGRectMake Method (x, y, width, height)
        custProfileNameLabel = [[[UITextField alloc] initWithFrame:CGRectMake(10, 11, 290, 21)] autorelease];
        custProfileNameLabel.tag = kCustomTextTag;
        custProfileNameLabel.textAlignment = UITextAlignmentLeft;
        [cell.contentView addSubview:custProfileNameLabel];
        custProfileNameLabel.backgroundColor = [UIColor clearColor];
        custProfileNameLabel.userInteractionEnabled = NO;
        custProfileNameLabel.placeholder = @"Custom Profile Name";
        custProfileNameLabel.autocapitalizationType = UITextAutocapitalizationTypeWords;
        custProfileNameLabel.clearButtonMode = UITextFieldViewModeWhileEditing;

        //UIKeyboardTypeDefault;
        cell.selectionStyle = UITableViewCellEditingStyleNone;
        CustomProfile *cProf = [CustomProfile alloc];
        cell.textLabel.text = [cProf tName];
    }

    if(indexPath.section == 1)
    {
        if(indexPath.row == ([[appDelegate serviceArray] count]) && self.editing){
            cell.textLabel.text = @"Add a Service";
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.accessoryType = UITableViewCellAccessoryNone;
            swService.hidden = YES;
            return cell;
        }else{
            swService.hidden = NO;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.textLabel.text = [[appDelegate serviceArray] objectAtIndex:indexPath.row];
        }
        [cell addSubview:swService];
    }

    if(indexPath.section == 2)
    {
        if(indexPath.row == ([[appDelegate connectArray] count]) && self.editing){
            cell.textLabel.text = @"Add a Connection";
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.accessoryType = UITableViewCellAccessoryNone;
            swConnect.hidden = YES;
            return cell;
        }else{
            swConnect.hidden = NO;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.textLabel.text = [[appDelegate connectArray] objectAtIndex:indexPath.row];
        }
        [cell addSubview:swConnect];
    }

    if(indexPath.section == 3)
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
        NSString *documentsDirectory = [paths objectAtIndex:0]; 
        NSString *myPlistPath = [documentsDirectory stringByAppendingPathComponent:@"ProfileManager.plist"]; 
        NSDictionary *plistDict = [[NSDictionary alloc] initWithContentsOfFile:myPlistPath]; 

        NSString *value;
        value = [plistDict objectForKey:@"Custom Ringtone"];

        if(indexPath.row == ([[appDelegate ringArray] count]) && self.editing){
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.accessoryType = UITableViewCellAccessoryNone;
        }else{
            cell.selectionStyle = UITableViewCellSelectionStyleBlue;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }

        cell.textLabel.text = value;
    }

    if(indexPath.section == 4)
    {
        if(indexPath.row == 0 && self.editing)
        {
            //cell.userInteractionEnabled = NO;
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.textLabel.text = @"Vibrate";
            swVibrate.hidden = YES;
        }else{
            swVibrate.hidden = NO;
            //cell.accessoryType = UITableViewCellAccessoryNone;    
            cell.selectionStyle = UITableViewCellSelectionStyleBlue;
            cell.textLabel.text = @"Vibrate";
        }   

        [cell.contentView addSubview:swVibrate];
    }

    return cell;
}   

截屏

在此处输入图像描述

4

2 回答 2

2

好吧,这个故障之所以成为可能是因为控件是在创建单元格时创建的,所以它只是在视图出现时继续创建额外的控件。

如果您也收到此故障,请确保在 ViewDidLoad 中创建控件,或调用 void,只是不要在 cellForRowAtIndexPath 中创建控件:

于 2009-11-30T06:23:48.497 回答
1

没有什么奇怪的事情发生。这是正常行为。注意线

  UITableViewCell *cell = dequeueReusableCellWithIdentifier ...
为了提高效率,tableview 控制器保留了以前使用过的单元格的队列,当您将 tableview 的新部分滚动到视图中时,它可能会返回一个以前使用过的单元格供您重用(在这种情况下,返回的值cell不会是nil)。如果该使用的单元格最初嵌入了一个 uiswitch,即使当前单元格不使用它,您也会再次获得该开关。

您需要检查您的单元分配代码是否返回给您的单元是现有的,如果它存在并且您不想要它,请移除开关。

霍华德

于 2011-04-27T15:43:43.537 回答