首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取更改了哪个开关时出现问题(附件类型为UISwitch)

获取更改了哪个开关时出现问题(附件类型为UISwitch)
EN

Stack Overflow用户
提问于 2011-07-26 11:44:57
回答 1查看 1.6K关注 0票数 0

我需要帮助找出哪个配件类型开关已更改。

我有一个UITableView中的游戏列表。我添加了一个UISwitch作为附件类型,每个开关对触摸做出反应,改变其状态并通知其动作接收器。

代码语言:javascript
复制
- (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:查看注释

代码语言:javascript
复制
- (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个不同的搜索查询,上面两个目标操作方法的例子是我找到的最好的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-26 11:54:28

尝尝这个

cellForRowAtIndexPath中设置一个类似于testSwitch.tag = indexpath.row的标记

然后在您的按钮单击事件中

代码语言:javascript
复制
UISwitch *tswitch = (UISwitch *)sender;

int row = tswitch.tag;

NSlog(@"Your selected array value :%@",[your array objectatindex:row]);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6825027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档