Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何判断哪些行切换开关已更改_Iphone_Objective C_Nsmutablearray_Uiswitch - Fatal编程技术网

Iphone 如何判断哪些行切换开关已更改

Iphone 如何判断哪些行切换开关已更改,iphone,objective-c,nsmutablearray,uiswitch,Iphone,Objective C,Nsmutablearray,Uiswitch,我有一个带有切换开关的附件视图的tableview。我指定了节和行,很难确定切换了哪一行。我使用了toggleSwitch.tag来获取indexRow,但是由于我的indexRow是IndexXPath.section的一部分,我不知道如何判断我切换了哪一行 代码如下: - (UITableViewCell *)tableAlert:(SBTableAlert *)tableAlert cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITab

我有一个带有切换开关的附件视图的tableview。我指定了节和行,很难确定切换了哪一行。我使用了toggleSwitch.tag来获取indexRow,但是由于我的indexRow是IndexXPath.section的一部分,我不知道如何判断我切换了哪一行

代码如下:

- (UITableViewCell *)tableAlert:(SBTableAlert *)tableAlert cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;

Category *cat = [allCategories objectAtIndex:indexPath.section];
Subject *sub = [cat.subjects objectAtIndex:indexPath.row];

cell = [[[SBTableAlertCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
UISwitch *toggleSwitch = [[UISwitch alloc] init];
cell.accessoryView = [[UIView alloc] initWithFrame:toggleSwitch.frame];
[cell.accessoryView addSubview:toggleSwitch];

cell.textLabel.text =sub.title;   
cell.detailTextLabel.text = sub.category_title;

if (sub.active==1){
    [toggleSwitch setOn:YES];
} else {
    [toggleSwitch setOn:NO];
}

toggleSwitch.tag = indexPath.row;

[toggleSwitch addTarget:self action:@selector(viewButtonPushed:) forControlEvents:UIControlEventValueChanged];
[toggleSwitch release];

return cell;

}


- (void)viewButtonPushed:(id)sender {
UIButton *button = (UIButton *)sender;    
UITableViewCell *cell = button.superview; // adjust according to your UITableViewCell-subclass' view hierarchy
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

Category *cat = [allCategories objectAtIndex:indexPath.section];
Subject *sub = [cat.subjects objectAtIndex:indexPath.row];

selectedSubject = sub;

UISwitch* switchControl = sender;
NSLog( @"The switch is %@", switchControl.on ? @"ON" : @"OFF" );

if(switchControl.on){
    [sub setActive:1];
    NSLog(@"%@ is being set to ACTIVE", selectedSubject.title);
}else{
    [sub setActive:0];
    NSLog(@"%@ is being set to INACTIVE", selectedSubject.title);
}

[sub setIsDirty:YES];

[cat.subjects replaceObjectAtIndex:indexPath.row withObject:sub];

[sub autorelease];
[cat autorelease];

}
这是我的didSelectRowAtIndexPath。我需要在这里引用切换开关吗

- (void)tableAlert:(SBTableAlert *)tableAlert didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

Category *cat = [allCategories objectAtIndex:indexPath.section];
Subject *sub = [cat.subjects objectAtIndex:indexPath.row];

selectedSubject = sub;
NSLog(@"selectedSubject = %@", selectedSubject.title);


if (tableAlert.type == SBTableAlertTypeMultipleSelct) {
    UITableViewCell *cell = [tableAlert.tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryNone)
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    else
        [cell setAccessoryType:UITableViewCellAccessoryNone];

    [tableAlert.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

}

我发现您需要转到单元格中项目的superview的superview(假设按钮或控件正好位于单元格的根之外),以获取指向单元格的指针

请尝试以下方法:

UITableViewCell *cell = button.superview.superview;
看看结果是否更好。有关更多信息,请查看我的博客帖子:


我发现您需要转到单元格中项目的superview的superview(假设按钮或控件正好位于单元格的根目录下),以获取指向该单元格的指针

请尝试以下方法:

UITableViewCell *cell = button.superview.superview;
看看结果是否更好。有关更多信息,请查看我的博客帖子:


如果要将更改保存在plist或类似文件中,您知道必须将更改写入磁盘。仅仅因为你改变了数组并不会改变文件本身。是的,我以后会这样做。在进一步的调试中,我发现我的问题不在于保存到数组中,而在于识别切换开关切换到了哪一行。请参阅我的答案,以获得一个简洁的方法:谢谢,这是更好的方法。你能看看我编辑过的帖子吗?我仍然有这个问题。@jrturton的解决方案非常好而且灵活!您知道,如果要将更改保存在plist或类似文件中,则必须将其写入磁盘。仅仅因为你改变了数组并不会改变文件本身。是的,我以后会这样做。在进一步的调试中,我发现我的问题不在于保存到数组中,而在于识别切换开关切换到了哪一行。请参阅我的答案,以获得一个简洁的方法:谢谢,这是更好的方法。你能看看我编辑过的帖子吗?我仍然有这个问题。@jrturton的解决方案非常好而且灵活!