2012-03-15 57 views
0

我有一個UINavigationControllerUITableViewController作爲根控制器。 UINavigationController位於UIPopoverController之內。 UITableViewController包含UITableViewCell(具有UISwitch)和UITableViewCell(具有UITableViewCellAccessoryDisclosureIndicator),當點擊時加載另一個UITableViewUITableView內的UISwitch

我的問題是,當在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]; 

}

這是一個非常棘手的問題,所以我需要你的幫助!

謝謝。

+0

您能提供* cellForRowAtIndexPath:*和* didSelectRowAtIndexPath:*的代碼嗎?你使用兩個不同的標識符來創建兩個單元格(我看* CellIdentifier1 *)? – 2012-03-15 09:47:55

+0

我編輯了我的問題。 – Giorgio 2012-03-15 10:14:32

+0

你是什麼意思與刷卡和點擊* UISwicth *?對我來說,選擇你的可選單元沒有區別。 – 2012-03-15 12:04:14

回答

0

我不知道如果可能是一個有效的解決方案,但你可以嘗試清除你的declarationChanged:選擇器中的表格單元格的選擇。

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

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

希望它有幫助。

+0

感謝您的幫助。我試過你的建議,但沒有運氣。我會嘗試創建一個重現問題的例子,我會發布它 – Giorgio 2012-03-15 13:05:12

+0

請喬治,請參閱我的編輯。 – 2012-03-15 14:26:53

+0

昨天我也試過用resignFirstResponder但沒用。無論如何,我解決了我的問題。在我的問題中查看我最近的評論。非常感謝你。 – Giorgio 2012-03-15 14:48:07