![](/img/trans.png)
[英]How to add subview to uiscrollview to start of content (before first subview)?
[英]Content add subview not working?
您今天過的怎么樣,希望一切順利
我的問題是我在uiableviewcell中有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];
}
首要的是,對於該開關,您有一個寬度和高度為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。
好吧,我發現了您的問題,請嘗試這樣做。 這里的問題是,添加開關后,您將標題賦予單元格標簽,這意味着標簽會隱藏您的開關。
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;
}
從您的評論中,我得知您正在使用情節提要板來創建表格視圖。 將開關添加到表格視圖原型單元格,並將標記設為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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.