内容添加子视图不起作用?

时间:2013-04-26 09:07:37

标签: ios xcode ipad uitableview uiswitch

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

我的问题是我在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 个答案:

答案 0 :(得分: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];
}

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

答案 1 :(得分:0)

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

答案 2 :(得分: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;

    }

答案 3 :(得分: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;


}