Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.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
Ios 每个分区表格行中的UI开关';s辅助视图_Ios_Uitableview_Xcode4.6_Uiswitch_Accessoryview - Fatal编程技术网

Ios 每个分区表格行中的UI开关';s辅助视图

Ios 每个分区表格行中的UI开关';s辅助视图,ios,uitableview,xcode4.6,uiswitch,accessoryview,Ios,Uitableview,Xcode4.6,Uiswitch,Accessoryview,我对iOS编码还不熟悉,我在SO中四处看看,尝试了几种方法来解决这个问题,但似乎什么都不管用。我是舒尔,这是因为我犯了一个愚蠢的错误:-) 我有一个分区表,共有大约25行,分为2个部分。 每个单元格都有一个开关。accessoryView,当开关状态发生变化时,我尝试将其存储在NSArray中,但在任何情况下,记录该索引处的数组内容都会奇怪地返回0。 (数组被实例化为TableViewController的属性(非原子,读写),并进行合成) 显然,当我上下滚动表格时,所有开关都会返回默认的OFF

我对iOS编码还不熟悉,我在SO中四处看看,尝试了几种方法来解决这个问题,但似乎什么都不管用。我是舒尔,这是因为我犯了一个愚蠢的错误:-)

我有一个分区表,共有大约25行,分为2个部分。 每个
单元格都有一个开关。accessoryView
,当开关状态发生变化时,我尝试将其存储在
NSArray
中,但在任何情况下,记录该索引处的数组内容都会奇怪地返回
0
。 (数组被实例化为
TableViewController
的属性
(非原子,读写)
,并进行合成)

显然,当我上下滚动表格时,所有开关都会返回默认的
OFF
状态

谢谢你的帮助

- (void)viewDidLoad
{
[super viewDidLoad];
//countries dict and switch state array init..  
self.countries = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"countries" ofType:@"plist"]];
NSNumber *b = [NSNumber numberWithBool:NO];
for (int i=0; i<210; i++) {
    [statiSwitch addObject:b];
    NSLog(@"%d", [[statiSwitch objectAtIndex:i] integerValue]);
}

//tableview drawing..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellWithSwitch";    
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

NSString *continent = [self tableView:tableView titleForHeaderInSection:indexPath.section];
NSString *country = [[self.countries valueForKey:continent] objectAtIndex:indexPath.row];

//Simple tag calculation...
NSInteger tagOb = indexPath.section * 100 + indexPath.row;

UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[mySwitch addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged];
mySwitch.tag = tagOb;
if ([statiSwitch count] > tagOb) {
    [mySwitch setOn:[[statiSwitch objectAtIndex:tagOb] boolValue] animated:NO];
     }

cell.textLabel.text = country;    
cell.accessoryView = mySwitch;

return cell;
}
  • 设置自定义单元格类
  • 在故事板中创建一个原型单元,并在其中放置一个开关
  • 将原型单元设置为自定义类,并设置重用标识符
  • 将交换机连接到自定义单元格类中的IBOutlet。 (如果您希望您的手机能够通信,还可以设置@protocol和delegate)
  • 现在让我们来看看棘手的部分:
  • 在-viewDidLoad中:

        for (int i = 0; i < [self.cellDataArray count]; ++i) {
                CustomCell *cell;
    
        cell = [self.tableView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:nil];
        cell.delegate = self;
        [self.cellsArray addObject:cell];
    }
    
    for(int i=0;i<[self.cellDataArray count];+i){
    自定义单元格*单元格;
    cell=[self.tableView dequeueReusableCellWithReuseIdentifier:@“cell”forindexath:nil];
    cell.delegate=self;
    [self.cellsArray addObject:cell];
    }
    
  • 在cellForRowAtIndexPath中,执行以下操作:

    cell=self.cellsArray[indexPath.row];//如果使用collectionView,则为.item

  • 由于开关连接到每个单元格,因此在CustomCell类中需要一个方法,该方法将基于开关执行某些操作,并且可能会向委托类发送调用


  • 我使用了一个简单的
    int数组
    来存储开关值,它似乎比由具有switchTag和switchState属性的对象组成的
    NSMutableArray
    工作得更好,这是我以前所做的

    代码如下:

    - (void)viewDidLoad
    {   
        //[here goes the code to retrieve my table data from a dictionary, getting sections and rows]
    
        //int array iniazialization, with zeros to have switches set at OFF at beginning
    
        for (int i = 0; i < 300; i++) {
        switchStates[i] = 0;
            }
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        static NSString *CellIdentifier = @"Cella"; 
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    //Cell drawing..
    
    NSString *continent = [self tableView:tableView titleForHeaderInSection:indexPath.section];
    NSString *country = [[self.countries valueForKey:continent] objectAtIndex:indexPath.row];
    cell.textLabel.text = country;
    
    //Calculate the tag to be assigned to the switches..
    
        switchTag = indexPath.section * 100 + indexPath.row
        UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
        [mySwitch addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged];
        mySwitch.tag = switchTag;
        mySwitch.on = switchStates[switchTag];
    
        cell.accessoryView = mySwitch;
    
        return cell;
    
    }
    
    //switch change action..
    
    - (void) switchChange:(UISwitch *)sender {
         switchStates[sender.tag] = sender.on;
    }
    
    -(void)viewDidLoad
    {   
    //[下面是从字典中检索我的表数据、获取节和行的代码]
    //int数组初始化,零在开始时将开关设置为OFF
    对于(int i=0;i<300;i++){
    开关状态[i]=0;
    }
    }
    -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
    {
    静态NSString*CellIdentifier=@“Cella”;
    UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    如果(单元格==nil){
    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
    }
    //单元格绘图。。
    NSString*Continental=[self tableView:tableView titleForHeaderInSection:indexPath.section];
    NSString*country=[[self.countries-valueForKey:containment]objectAtIndex:indexath.row];
    cell.textlab.text=国家/地区;
    //计算要分配给开关的标签。。
    switchTag=indexPath.section*100+indexPath.row
    UISwitch*mySwitch=[[UISwitch alloc]initWithFrame:CGRectZero];
    [mySwitch addTarget:self action:@selector(switchChange:)for ControlEvents:UIControlEventValueChanged];
    mySwitch.tag=开关标签;
    mySwitch.on=switchStates[switchTag];
    cell.accessoryView=mySwitch;
    返回单元;
    }
    //开关更改操作。。
    -(无效)开关更改:(UISwitch*)发送器{
    switchStates[sender.tag]=sender.on;
    }
    

    现在,我已经“仅仅”将这些开关值存储在核心数据中。。下面的问题见:-)

    我想您在向statiSwitch添加对象之前忽略了初始化statiSwitch

    在for循环之前的viewDidLoad中,添加此行

    statiSwitch = [NSMutableArray array];
    

    尝试子类化UITableViewCell,并将整个单元格存储在数组中,而不仅仅是开关值。然后,当tableView滚动时,将单元格从数组中拉出,而不是退出队列。因此,我应该设置一个包含所有(比如)20个单元格对象的数组,包括附件开关,然后使用
    -(UITableViewCell*)tableView:(UITableView*)tableView单元格for RowAtIndexPath:(NSIndexPath*)indexPath
    方法将单元格从该数组中取出?您应该将UITableViewCell子类化,并在该类中处理切换操作。这就像使用对象类存储数据一样。好的,我来试试!谢谢(如果你给我推荐一些处理切换操作的子类的示例代码..那就太好了:-)你是在编写故事板,还是在编写代码?对于故事板,这是一个非常简单的过程。非常感谢!我会试着让你知道的。我试过这样做,对单元格数组和其他的都没问题,但是我有编译器警告,行
    cell=[self.tableView dequeueReusableCellWithReuseIdentifier:@“cell”forIndexPath:nil]
    ,将其放置在
    -viewDidLoad:
    方法中。它告诉我,
    tableView
    没有
    dequeueReusableCellWithReuseIdentifier:
    的任何标识符。我通过一种变通方法解决了这个问题,使用一个简单的int数组来存储开关值。。不知道在NSObject的NSMutableArray中存储值不起作用。。有关详细信息,请参阅我的答案。如果您将@“Cell”更改为@“CellWithSwitch”,您就不会有这个问题。在我的答案中,我写了“@Cella”而不是“@CellWithSwitch”,因为这是我在情节提要和编辑的“table view controller”中使用的新标识符。m文件我将继续使用int数组。。但是你是对的@Rick,我忘了在上面的代码中初始化数组:
    statiSwitch
    被声明为
    viewController
    的一个属性,并进行了合成等等,这就是为什么我认为没有必要再次对其进行分配和序列化。不用担心,请记住这一点。在许多情况下,您仍然需要使用NSMutableArray。如果你想学习iOS编程,你不能总是坚持使用C
    statiSwitch = [NSMutableArray array];