0

你今天过得怎么样,希望一切顺利

我的问题是我在 uitableviewcell 中有 uiswitch 。但是,当我使用 contentview 将开关添加为单元格中的子视图时,它不会出现在我的单元格上吗?请问有人知道为什么它们没有出现在每个自定义单元格上吗?提前致谢 。

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    UISwitch *switchController = nil ;

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:@"Cell"];

      switchController=  [[UISwitch alloc] initWithFrame:CGRectZero];
        switchController.tag=100;
        [cell.contentView addSubview:switchController];
        CGRect switchFrame = switchController.frame;
        switchFrame.origin.x = 50.0f;
        switchFrame.origin.y = 10.0f;
        switchController.frame = switchFrame;
        [switchController addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];

    }

    else
    {
        switchController =(UISwitch *)[cell.contentView viewWithTag:100];

    }

//This for persist switch state when scroll up or down 
    if ([[[self.SwitchArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row ]isEqualToString:@"ON"])
    {

        switchController.on=YES;

    }
    else
    {
        switchController.on=NO;
    }

   NSString *value = [[mainArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    cell.textLabel.text = value;
    cell.textLabel.textAlignment = NSTextAlignmentRight;
    cell.textLabel.font = [UIFont systemFontOfSize:15.0];

    return cell;

}

这是 SwitchChanged 的​​代码:

-(void)switchChanged:(UISwitch *)sender
{
    UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath *index=[mainTableView indexPathForCell:cell];

    if (sender.on)
    {

        [[self.SwitchArray objectAtIndex:index.section] replaceObjectAtIndex:index.row withObject:@"ON"];

    }
    else
    {
        //Update my switch states array 
        [[self.SwitchArray objectAtIndex:index.section] replaceObjectAtIndex:index.row withObject:@"OFF"];

   }

    [padFactoids setObject:[NSKeyedArchiver archivedDataWithRootObject:SwitchArray] forKey:@"savedArray"];
    [padFactoids synchronize];

}
4

4 回答 4

0

好吧,我发现了您的问题,像这样尝试它会起作用。这里的问题是,在添加开关后,您将标题赋予单元格标签,这意味着标签隐藏了您的开关。

Problem: cell.textLabel 隐藏开关。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

        UISwitch *switchController = nil ;

        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                          reuseIdentifier:@"Cell"];

        NSString *value = [[mainArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        cell.textLabel.text = value;
        cell.textLabel.textAlignment = NSTextAlignmentRight;
        cell.textLabel.font = [UIFont systemFontOfSize:15.0];
          switchController=  [[UISwitch alloc] initWithFrame:CGRectZero];
            switchController.tag=100;
            [cell.contentView addSubview:switchController];
            CGRect switchFrame = switchController.frame;
            switchFrame.origin.x = 50.0f;
            switchFrame.origin.y = 10.0f;
            switchController.frame = switchFrame;
            [switchController addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];

        }

        else
        {
            switchController =(UISwitch *)[cell.contentView viewWithTag:100];

        }

    //This for persist switch state when scroll up or down 
        if ([[[self.SwitchArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row ]isEqualToString:@"ON"])
        {

            switchController.on=YES;

        }
        else
        {
            switchController.on=NO;
        }



        return cell;

    }
于 2013-04-26T09:18:14.473 回答
0

从您的评论中,我了解到您正在使用情节提要来创建表格视图。将开关添加到表格视图原型单元格并将标记设为 100。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
 UISwitch *switchController =(UISwitch *)[cell viewWithTag:100];

 [switchController addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];

//This for persist switch state when scroll up or down 
if ([[[self.SwitchArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row ]isEqualToString:@"ON"])
{

    switchController.on=YES;

}
else
{
    switchController.on=NO;
}

 NSString *value = [[mainArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.text = value;
cell.textLabel.textAlignment = NSTextAlignmentRight;
cell.textLabel.font = [UIFont systemFontOfSize:15.0];

return cell;


}
于 2013-04-26T09:40:28.503 回答
0

第一件事是,您有一个宽度和高度为 0 的框架用于该开关。其次,如果您使用带有原型单元的情节提要,它可能根本不会进入if (cell == nil). 希望这可以帮助。

编辑:

将故事板中的开关添加到原型单元会更容易,但是如果您希望以编程方式将其移到 if 之外并添加如下条件:

UISwitch *switchController = (UISwitch *)[cell viewWithTag:100];
if (!switchController) {
      switchController=  [[UISwitch alloc] initWithFrame:CGRectZero];
      switchController.tag=100;
      [cell.contentView addSubview:switchController];
      CGRect switchFrame = switchController.frame;
      switchFrame.origin.x = 50.0f;
      switchFrame.origin.y = 10.0f;
      switchController.frame = switchFrame;
      [switchController addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
}

当然,不要忘记设置宽度和高度。

于 2013-04-26T09:12:29.247 回答
0

您的 switchController 宽度和高度为 0。您首先使用 CGRectZero 初始化开关,然后更改原点坐标,但宽度和高度仍为 0。

于 2013-04-26T09:15:12.153 回答