我需要帮助找出哪个配件类型开关已更改。
我有一个UITableView中的游戏列表。我添加了一个UISwitch作为附件类型,每个开关对触摸做出反应,改变其状态并通知其动作接收器。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
if(nil == cell){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"cell"]autorelease];
}
Games *games=[appDelegate.hidden objectAtIndex:indexPath.row];
cell.textLabel.text = games.gameName;
//the switch is defined here, as is its target-action
testSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0, 0.0, 74.0, 27.0)];
[testSwitch addTarget:self action:@selector(button_Clicked:)
forControlEvents:UIControlEventValueChanged];
//If I used the tags
testSwitch.tag=indexPath.row;
[testSwitch setOn:YES];
cell.accessoryView=testSwitch;
[games release];
return cell;
}和target-action:查看注释
- (void)button_Clicked:(id)sender{
UISwitch* switchControl = sender; //if I make this an int, the switches
//have different numbers. If it's cast as a string, it's gobbledegook.
//This always returns 0. I can't tell it it's not getting anything (ie it's
// nil), or if the row always comes up 0.
UITableViewCell *cell = (UITableViewCell *)[sender superview];
NSIndexPath *x=[table indexPathForCell:cell];
NSLog(@"%i",x.row);
}我看到了两个潜在的问题。首先,我需要为每一行做一个唯一命名的切换。第二,我找不到手机了。
如果任何人能看到我的错误,欢迎任何帮助。
我浏览了我在stackoverflow上能找到的所有东西,在Google上找到了5到6个不同的搜索查询,上面两个目标操作方法的例子是我找到的最好的。
发布于 2011-07-26 11:54:28
尝尝这个
在cellForRowAtIndexPath中设置一个类似于testSwitch.tag = indexpath.row的标记
然后在您的按钮单击事件中
UISwitch *tswitch = (UISwitch *)sender;
int row = tswitch.tag;
NSlog(@"Your selected array value :%@",[your array objectatindex:row]);https://stackoverflow.com/questions/6825027
复制相似问题