UISwitch出现在其他细胞上!毛刺?

时间:2009-11-21 07:09:44

标签: ios uitableview uiswitch

我在表格中有5个单独的部分,其中2个部分具有可自定义的单元格,当处于编辑模式时,其中2个部分会生成一个附加单元格,该单元格将向列表添加新单元格。我的问题是,当我调用编辑模式时,UISwitch出现故障!并决定跳转,非常简单的代码,只是没有IDEA为什么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;
}   

截图

enter image description here

2 个答案:

答案 0 :(得分:2)

嗯,这个故障之所以成为可能,是因为在创建单元格时创建了控件,所以它只是在视图出现时继续创建其他控件。

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

答案 1 :(得分:1)

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

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

您需要检查您的手机信息分配代码,无论您回馈给您的手机是否为现有手机,如果它存在且您不想要它,请将其移除。

霍华德