如何检索UISwitch的UITableView行号? [英] How do I retrieve UITableView row number of a UISwitch?
问题描述
我已经尝试过这里发布的几种方法,但是我无法让我的表充满开关以返回已更改开关的单元格的索引值。我正在以编程方式创建包含表的视图(无xib)。
TableSandboxAppDelegate.m 我在 didFinishLaunchingWithOptions:
with:
...
TableSandboxViewController * sandboxViewController = [[TableSandboxViewController] alloc]
init];
[[自我窗口] 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;
}其他{
theSwitch.on = NO;
}
返回单元格;
TableViewController.m - (IBAction)switchChanged :(UISwitch *)发件人
读取:
UITableViewCell * theParentCell = [[sender superview] superview] ;
NSIndexPath * indexPathOfSwitch = [self.tableView indexPathForCell:theParentCell];
NSLog(@Switch在索引处更改:%d,indexPathOfSwitch.row);
我的日志结果始终是Switch index at index:0。我觉得问题出在CGPoint系列中,我尝试过对发送者([发送者超级视图],[[发送者超级视图]超级视图]等的替换组合)。我不觉得那条线指向显示表格的视图。
我做错了什么?
注意添加10 / 9,9:15 EDT: 我的目标是能够处理表中大约100个是/否的问题,因此重用是关键。我想滚动并让表格成为每个开关的状态,并且能够在离开视图时检索它们。
标签是一个不错的解决方案,但有点笨拙,因为单元格 - 以及它们的子视图 - 不断被重用,改变它们的行 - 因此它们需要的标签。
<相反,我通常保留其中一个:
- (NSIndexPath *)indexPathWithSubview:(UIView *)subview {
while(![subview isKindOfClass:[UITableViewCell self]]&& subview){
subview = subview.superview;
}
return [self.tableView indexPathForCell:(UITableViewCell *)subview];
}
然后当我得到IBAction时:
- (IBAction)someSubviewAction:(id)sender {
NSIndexPath * indexPath = [self indexPathWithSubview:(UIView *)sender];
//从这里继续
}
I have tried several approaches posted here, but I cannot get my table full of switches to return an index value for the cell of the changed switch. I am creating the view containing the table programmatically (no xib).
TableSandboxAppDelegate.m I instantiate the view controller in didFinishLaunchingWithOptions:
with:
...
TableSandboxViewController *sandboxViewController = [[TableSandboxViewController alloc]
init];
[[self window] setRootViewController:sandboxViewController];
...
TableViewController.h file reads:
@interface TableSandboxViewController : UITableViewController
{
NSMutableArray *_questionOrder;
NSMutableArray *switchStates;
}
@end
TableViewController.m cellForRowAtIndexPath:
reads:
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
reads:
UITableViewCell *theParentCell = [[sender superview] superview];
NSIndexPath *indexPathOfSwitch = [self.tableView indexPathForCell:theParentCell];
NSLog(@"Switch changed at index: %d", indexPathOfSwitch.row);
My log result is always "Switch changed at index: 0". I feel like the problem is in that CGPoint line where I've tried combinations of replacements for "sender" ([sender superview], [[sender superview]superview], etc). I don't feel like that line is pointing to the view that displays the table.
What am I doing wrong?
Note added 10/9, 9:15 EDT: my goal is to be able to handle about 100 yes/no questions in the table, so reuse is a key. I want to scroll and have the table the state of each switch, as well as be able to retrieve them when leaving the view.
Tags is an okay solution, but a little clumsy because the cells - and therefore their subviews - are continually being reused, changing their rows - and therefore the tags they need.
Instead, I generally keep one of these around:
- (NSIndexPath *)indexPathWithSubview:(UIView *)subview {
while (![subview isKindOfClass:[UITableViewCell self]] && subview) {
subview = subview.superview;
}
return [self.tableView indexPathForCell:(UITableViewCell *)subview];
}
Then when I get an IBAction:
- (IBAction)someSubviewAction:(id)sender {
NSIndexPath *indexPath = [self indexPathWithSubview:(UIView *)sender];
// carry on from here
}
这篇关于如何检索UISwitch的UITableView行号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!