【发布时间】:2013-10-16 05:47:29
【问题描述】:
我已经尝试了这里发布的几种方法,但我无法让我的表充满开关来返回已更改开关的单元格的索引值。我正在以编程方式创建包含表的视图(没有 xib)。
TableSandboxAppDelegate.m 我在didFinishLaunchingWithOptions: 中实例化视图控制器:
...
TableSandboxViewController *sandboxViewController = [[TableSandboxViewController alloc]
init];
[[self window] setRootViewController:sandboxViewController];
...
TableViewController.h 文件内容如下:
@interface TableSandboxViewController : UITableViewController
{
NSMutableArray *_questionOrder;
NSMutableArray *switchStates;
}
@end
TableViewController.m cellForRowAtIndexPath: 读取:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
UISwitch *theSwitch = nil;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"MainCell"];
theSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
theSwitch.tag = 100;
[theSwitch addTarget:self action:@selector(switchChanged:)
forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:theSwitch];
} else {
theSwitch = [cell.contentView viewWithTag:100];
}
if ([[switchStates objectAtIndex:indexPath.row] isEqualToString:@"ON"]) {
theSwitch.on = YES;
} else {
theSwitch.on = NO;
}
return cell;
TableViewController.m -(IBAction)switchChanged:(UISwitch *)sender 读取:
UITableViewCell *theParentCell = [[sender superview] superview];
NSIndexPath *indexPathOfSwitch = [self.tableView indexPathForCell:theParentCell];
NSLog(@"Switch changed at index: %d", indexPathOfSwitch.row);
我的日志结果始终是“Switch changed at index: 0”。我觉得问题出在 CGPoint 行中,我尝试了“sender”([sender superview]、[[sender superview]superview] 等)的替换组合。我不觉得那条线指向显示表格的视图。
我做错了什么?
在美国东部夏令时间 10 月 9 日 9:15 添加注释: 我的目标是能够处理表中大约 100 个是/否问题,因此重用是关键。我想滚动并让表格了解每个开关的状态,并在离开视图时能够检索它们。
【问题讨论】:
标签: iphone ios objective-c uitableview uiswitch