【发布时间】:2012-03-15 09:33:24
【问题描述】:
我有一个UINavigationController 和一个UITableViewController 作为根控制器。 UINavigationController 在 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];
}
这是一个非常棘手的问题,所以我需要你的帮助!
谢谢。
【问题讨论】:
-
能否提供 cellForRowAtIndexPath: 和 didSelectRowAtIndexPath: 的代码?您是否使用两个不同的标识符来创建两个单元格(我看到 CellIdentifier1)?
-
滑动并点按 UISwicth 是什么意思?对我来说,选择可选择的单元格没有区别。
-
如果我在 UISwitch 上按下并移动手指以更改其状态,然后点击另一个单元格,则不会调用 didSelectRowAtIndexPath。如果我点击 UISwitch 以更改其状态,然后点击另一个单元格,则会调用 didSelectRowAtIndexPath。看来问题出在滑动上。我想指出,带有我的 UITableViewController 的 UINavigationController 位于 UIPopoverController 内。
-
好的。我解决了我的问题。我不明白为什么,但是如果我以编程方式而不是通过 Nib 文件创建 UITableViewController,则不存在此问题。 Flex_Addicted,感谢您的帮助。
标签: ios uitableview uiswitch didselectrowatindexpath