UITwitch在UITableViewCell中

时间:2012-03-15 09:33:25

标签: ios uitableview uiswitch didselectrowatindexpath

我有UINavigationController作为根控制器UITableViewControllerUINavigationController位于UIPopoverController内。 UITableViewController包含UITableViewCell(带有UISwitch)和UITableViewCell(带有UITableViewCellAccessoryDisclosureIndicator),在点按时会加载另一个UITableView

我的问题是,当在UISwitch上轻扫然后点按其他单元格时,不会调用didSelectRowAtIndexPath。如果我再次点击被叫。如果我点按UISwitch,而不是滑动,则不会有任何问题。

问题仅出在iOS4上。在iOS5上工作得很好!

我的cellForRowAtIndexPath方法是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier1 = @"Cell1";
static NSString *CellIdentifier2 = @"Cell2";

// Configure the cell...
NSUInteger row = [indexPath row];
UITableViewCell *cell = nil;
switch (row) {
    case 0: {
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text = [controllers objectAtIndex:row];
        cell.accessoryType = UITableViewCellAccessoryNone;
        UITextField *passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(160, 11, 400, 30)];
        passwordTextField.adjustsFontSizeToFitWidth = YES;
        passwordTextField.placeholder = @"Richiesta";
        passwordTextField.keyboardType = UIKeyboardTypeDefault;
        passwordTextField.secureTextEntry = YES;
        passwordTextField.tag = 100;
        [passwordTextField addTarget:self action:@selector(passwordChanged:) forControlEvents:UIControlEventEditingChanged];
        passwordTextField.text = password;
        [cell addSubview:passwordTextField];
        [passwordTextField release];
        break;
    }

    case 1: {
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text = [controllers objectAtIndex:row];
        cell.accessoryType = UITableViewCellAccessoryNone;
        UITextField *pinTextField = [[UITextField alloc] initWithFrame:CGRectMake(160, 11, 400, 30)];
        pinTextField.adjustsFontSizeToFitWidth = YES;
        pinTextField.placeholder = @"Richiesta";
        pinTextField.keyboardType = UIKeyboardTypeDefault;
        pinTextField.secureTextEntry = YES;
        pinTextField.tag = 101;
        [pinTextField addTarget:self action:@selector(pinChanged:) forControlEvents:UIControlEventEditingChanged];
        pinTextField.text = aspin;
        [cell addSubview:pinTextField];
        [pinTextField release];
        break;
    }
    case 2: {
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier2] autorelease];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        UITableViewController *controller = [controllers objectAtIndex:row];
        cell.textLabel.text = controller.title;
        cell.detailTextLabel.text = @"Busta Crittografica P7M (CAdES)";
        envelopeType = cell.detailTextLabel.text;
        break;
    }
    case 3: {
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
        }
        cell.textLabel.text = @"Dichiaro di aver preso visione del documento, di sottoscriverne il contenuto e di essere consapevole della validità legale della firma apposta";
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.numberOfLines = 5;
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        //add a switch
        UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
        [switchview addTarget:self action:@selector(declarationChanged:) forControlEvents:UIControlEventValueChanged];
        switchview.on = declaration;
        cell.accessoryView = switchview;
        [switchview release];
        break;
    }
}
return cell;

}

我的didSelectRowAtIndexPath方法是:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
switch (row) {
    case 0: {
        [[[tableView cellForRowAtIndexPath:indexPath] viewWithTag:100] becomeFirstResponder];
        break;   
    }
    case 1: {
        [[[tableView cellForRowAtIndexPath:indexPath] viewWithTag:101] becomeFirstResponder];
        break;   
    }
    case 2: {
    ((CheckListController *)[self.controllers objectAtIndex:2]).contentSizeForViewInPopover = self.view.bounds.size;
        self.contentSizeForViewInPopover = self.view.bounds.size;

        [self.navigationController pushViewController:[self.controllers objectAtIndex:2]
                                             animated:YES];
        break;   
    }
    default:
        break;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

这是一个非常棘手的问题,所以我需要你的帮助!

感谢。

1 个答案:

答案 0 :(得分:0)

我不知道是否可以成为有效的解决方案,但您可以尝试清除declarationChanged:选择器中的表格单元格的选择。

- (void)declarationChanged:(id)sender
{
  // do your stuff here...
  //[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:animated];

  UISwitch* s = (UISwicth*)sender;
  [s resignFirstResponder];
}

希望它有所帮助。