dmc*_*all 5 iphone objective-c uitableview uiswitch ios
我已经尝试过这里发布的几种方法,但是我无法让我的表充满交换机来返回已更改交换机的单元格的索引值.我正在以编程方式创建包含表的视图(无xib).
TableSandboxAppDelegate.m我实例化视图控制器didFinishLaunchingWithOptions::
...
TableSandboxViewController *sandboxViewController = [[TableSandboxViewController alloc]
init];
[[self window] setRootViewController:sandboxViewController];
...
Run Code Online (Sandbox Code Playgroud)
TableViewController.h文件读取:
@interface TableSandboxViewController : UITableViewController
{
NSMutableArray *_questionOrder;
NSMutableArray *switchStates;
}
@end
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
我的日志结果始终是"Switch index at index:0".我觉得问题出在CGPoint系列中,我尝试过"发送者"([发送者超级视图],[[发送者超级视图]超级视图]等的替换组合).我不觉得那条线指向显示表格的视图.
我究竟做错了什么?
注释添加10/9,9:15 EDT:我的目标是能够在表中处理大约100个是/否问题,因此重用是关键.我想滚动并让表格成为每个开关的状态,并且能够在离开视图时检索它们.
dan*_*anh 11
标签是一个好的解决方案,但有点笨拙,因为单元格 - 以及它们的子视图 - 不断被重用,改变它们的行 - 因此它们需要的标签.
相反,我通常会保留其中一个:
- (NSIndexPath *)indexPathWithSubview:(UIView *)subview {
while (![subview isKindOfClass:[UITableViewCell self]] && subview) {
subview = subview.superview;
}
return [self.tableView indexPathForCell:(UITableViewCell *)subview];
}
Run Code Online (Sandbox Code Playgroud)
然后,当我得到一个IBAction:
- (IBAction)someSubviewAction:(id)sender {
NSIndexPath *indexPath = [self indexPathWithSubview:(UIView *)sender];
// carry on from here
}
Run Code Online (Sandbox Code Playgroud)
您可以将开关视图标记设置为行索引.代替theSwitch.tag = 100;
做
-(UITableViewCell*)tableView:table cellForRowAtIndexPath:indexPth
{
UISwitch *theSwitch = nil;
if (cell == nil) {
...
// as per your example
[cell.contentView addSubview:theSwitch];
} else {
theSwitch = subviewWithClass(cell.contentView, [UISwitch class]);
}
theSwitch.tag = indexPath.row;
...
}
Run Code Online (Sandbox Code Playgroud)
添加此辅助函数以替换viewWithTag:调用
UIView *subviewWithClass(UIView *contentview, Class klass)
{
for (UIView *view in contentview.subviews)
if ([view isKindOfClass:klass])
return view;
return nil;
}
Run Code Online (Sandbox Code Playgroud)
然后在switchChanged函数中检索标记,即现在的行索引
-(IBAction)switchChanged:(UISwitch *)sender {
NSLog(@"Selected Switch - %d", sender.tag);
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2291 次 |
| 最近记录: |