【问题标题】:Content add subview not working?内容添加子视图不起作用?
【发布时间】:2013-04-26 09:07:37
【问题描述】:

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

我的问题是我在 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];

}

【问题讨论】:

  • 为什么要添加到单元格的 contentView 中,将其添加到单元格中......
  • 将开关添加到情节提要单元格.. 看我的答案

标签: ios xcode ipad uitableview uiswitch


【解决方案1】:

从您的 cmets 中,我了解到您正在使用情节提要来创建表格视图。将开关添加到表格视图原型单元格,并将标签设为 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;


}

【讨论】:

  • 是的,我喜欢这个,但仍然没有出现。感谢您的宝贵时间
  • 只有开关没有出现?
  • 你在为单元格使用自定义类吗?
【解决方案2】:

你的switchController宽高为0。你先用CGRectZero初始化开关,然后你改变原点坐标但宽高还是0。

【讨论】:

  • 感谢您的快速回复。我确实改变了我的开关的宽度和高度,但仍然没有出现
【解决方案3】:

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

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;

    }

【讨论】:

  • 非常感谢。但是问题仍然存在
  • 你还面临同样的问题吗?
  • 是的,你能帮忙吗?
【解决方案4】:

第一件事是,您有一个宽度和高度为 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];
}

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

【讨论】:

  • 感谢您的回复。是的,你是对的,我在我的手机上使用故事板。那么如何解决这个问题,如果(cell == nil)我需要输入这个子句,因为我每个都有可重用的带有开关的单元格?请帮助您成为救生员@Levi
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多