Ios 内容添加子视图不工作?
你今天过得怎么样,希望一切顺利 我的问题是uitableviewcell中有uiswitch。但是,当我使用contentview将开关添加为单元格中的子视图时,它不会出现在我的单元格中?任何人,请你知道为什么他们没有出现在每个自定义单元格?提前谢谢 这是我的密码:Ios 内容添加子视图不工作?,ios,xcode,ipad,uitableview,uiswitch,Ios,Xcode,Ipad,Uitableview,Uiswitch,你今天过得怎么样,希望一切顺利 我的问题是uitableviewcell中有uiswitch。但是,当我使用contentview将开关添加为单元格中的子视图时,它不会出现在我的单元格中?任何人,请你知道为什么他们没有出现在每个自定义单元格?提前谢谢 这是我的密码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITabl
- (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;
}
以下是交换机的代码:
-(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];
}
第一件事是,你有一个宽度和高度为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];
}
当然,不要忘记设置宽度和高度。您的switchController宽度和高度为0。首先使用CGRectZero初始化开关,然后更改原点坐标,但宽度和高度仍然为0。我发现了您的问题,请这样尝试,它会工作的。 这里的问题是,在添加开关后,您为单元格标签指定了标题,这意味着标签隐藏了开关
问题
:cell.textlab隐藏开关
- (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;
}
从您的评论中,我知道您正在使用故事板来创建表视图。将开关添加到表视图原型单元,并将标记设为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;
}
为什么要添加到单元格的contentView将其添加到单元格..将开关添加到情节提要单元格。。见我的答案谢谢你的快速回复。我确实改变了开关的宽度和高度,但仍然没有出现。谢谢您的回复。是的,你是对的,我在手机上使用故事板。那么这怎么解决呢,如果(cell==nil),我需要输入这个子句,因为我在每个单元格中都有带开关的可重用单元格?请帮助你将成为一名救生员@LeviThank非常感谢你。但是问题仍然存在,你仍然面临着同样的问题?是的,你能帮忙吗?如果你把上面的代码按原样写,那么它就会来,因为我用这个代码检查过。是的,我喜欢这个,但仍然没有出现。感谢您的timeOnly开关没有出现?您是否正在为单元格使用自定义类?