UISwitch在UITableview(iOS 8)中不可见?
我在UITableview中创建了一个UISwitch,但它在我在项目中引用和实现的确切行中不可见。有人能告诉我如何更正它吗UISwitch在UITableview(iOS 8)中不可见?,ios,uitableview,uiswitch,Ios,Uitableview,Uiswitch,我在UITableview中创建了一个UISwitch,但它在我在项目中引用和实现的确切行中不可见。有人能告诉我如何更正它吗 if (indexPath.row == 1) { UISwitch *switchBtn = [[UISwitch alloc] initWithFrame:CGRectMake(200, 10, 150, 50)]; [switchBtn setOn:YES animated:YES]; NSInteger row; switchBtn.tag=row;
if (indexPath.row == 1) {
UISwitch *switchBtn = [[UISwitch alloc] initWithFrame:CGRectMake(200, 10, 150, 50)];
[switchBtn setOn:YES animated:YES];
NSInteger row;
switchBtn.tag=row;
row =[indexPath row];
[switchBtn addTarget:self action:@selector(switchStateChanged:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = switchBtn;
[cell addSubview:switchBtn];
UILabel *lblPhone =[[UILabel alloc]initWithFrame:CGRectMake(5, 5, 230, 30)];
lblPhone.text = @"Change Default View as Month View";
lblPhone.font = [UIFont systemFontOfSize:12];
[cell.contentView addSubview:lblPhone];
}
-(IBAction)switchStateChanged:(id)sender
{
UISwitch *switchState = sender;
if(switchState.tag == 0)
{
NSLog(@"ON");
}
else {
NSLog(@"OFF");
}
}
您的许多代码都毫无意义。例如,
[switchBtn设置:是动画:是]当您的开关按钮还不可见时(即,尚未插入到视图层次中,将不会出现动画),代码>将毫无意义
接下来,您声明一个没有值的变量行
,然后将开关的标记分配给它??结果可能未定义,并且肯定不会实现任何对您有意义的结果。如果您只需要indexPath
行,那么这一切有什么意义呢
接下来,将开关按钮指定给附件视图,然后将其添加为单元格的子视图!两个视图中的一个当然是不必要的。附件视图可能应该保留为真正的“附件”功能(请阅读)
但对我来说,最大的问题是如何处理单元重用,你的代码对此并不清楚。你必须知道单元是重用的([tableView dequeueReusableCell…
方法)。为一行创建的单元格,稍后可由另一行中的表视图实例重用。常见的错误是为单元格创建特定内容,希望它停留在第一次出现的位置
在UITableView委托方法中,您必须为当前正在考虑的行设置该特定单元格的所有属性。也就是说,还要确保其他行的单元格中不存在switchButton
这很难解释,但你应该发布整个方法实现,让我们有一个更好的想法。你的许多代码没有什么意义。例如,[switchBtn setOn:YES animated:YES];
当你的开关按钮还不可见时就毫无意义了(即,如果尚未插入到视图层次中,则不会出现动画
接下来,您声明一个没有值的变量行
,然后将开关的标记分配给它??结果可能未定义,并且肯定不会实现任何对您有意义的结果。如果您只需要indexPath
行,那么这一切有什么意义呢
接下来,将开关按钮指定给附件视图,然后将其添加为单元格的子视图!两个视图中的一个当然是不必要的。附件视图可能应该保留为真正的“附件”功能(请阅读)
但对我来说,最大的问题是如何处理单元重用,你的代码对此并不清楚。你必须知道单元是重用的([tableView dequeueReusableCell…
方法)。为一行创建的单元格,稍后可由另一行中的表视图实例重用。常见的错误是为单元格创建特定内容,希望它停留在第一次出现的位置
在UITableView委托方法中,您必须为当前正在考虑的行设置该特定单元格的所有属性。也就是说,还要确保其他行的单元格中不存在switchButton
这很难解释,但您应该发布整个方法实现,以便我们有更好的想法。参考此代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
if (indexPath.row == 1) {
UISwitch *switchBtn = [[UISwitch alloc] init];
[switchBtn setOn:YES animated:NO];
switchBtn.tag=[indexPath row];
[switchBtn addTarget:self action:@selector(switchStateChanged:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = switchBtn;
UILabel *lblPhone =[[UILabel alloc]initWithFrame:CGRectMake(5, 5, 230, 30)];
lblPhone.text = @"Change Default View as Month View";
lblPhone.font = [UIFont systemFontOfSize:12];
[cell.contentView addSubview:lblPhone];
}
// Configure the cell...
return cell;
}
-(void)switchStateChanged:(id)sender
{
UISwitch *switchControl = sender;
if(switchControl.tag == 1)
{
if (switchControl.isOn) {
NSLog(@"on");
}else{
NSLog(@"off");
}
}
}
截屏
请参阅此代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
if (indexPath.row == 1) {
UISwitch *switchBtn = [[UISwitch alloc] init];
[switchBtn setOn:YES animated:NO];
switchBtn.tag=[indexPath row];
[switchBtn addTarget:self action:@selector(switchStateChanged:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = switchBtn;
UILabel *lblPhone =[[UILabel alloc]initWithFrame:CGRectMake(5, 5, 230, 30)];
lblPhone.text = @"Change Default View as Month View";
lblPhone.font = [UIFont systemFontOfSize:12];
[cell.contentView addSubview:lblPhone];
}
// Configure the cell...
return cell;
}
-(void)switchStateChanged:(id)sender
{
UISwitch *switchControl = sender;
if(switchControl.tag == 1)
{
if (switchControl.isOn) {
NSLog(@"on");
}else{
NSLog(@"off");
}
}
}
截屏
在您的代码中,只需将150替换为50即可
UISwitch *switchBtn = [[UISwitch alloc] initWithFrame:CGRectMake(200, 10, 50, 50)];
再加上这一行
[cell.contentView addSubview:switchBtn];
并评论这句话
//cell.accessoryView=switchBtn;
在代码中,只需将150替换为50即可
UISwitch *switchBtn = [[UISwitch alloc] initWithFrame:CGRectMake(200, 10, 50, 50)];
再加上这一行
[cell.contentView addSubview:switchBtn];
并评论这句话
//cell.accessoryView=switchBtn;
该开关也可以添加到附件视图中,只需在情节提要中添加UITableView,而无需在其中拖动UITableViewCell(不添加UITableViewCell)。这样,在上面的代码中,如果(cell==nil)将调用条件。下面的代码适用于我,将accesoryView作为UISwitch
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
if(indexPath.row==3){
UISwitch *eventSwitch=[[UISwitch alloc] initWithFrame:CGRectZero];
[eventSwitch addTarget:self action:@selector(toggleEventNotification:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView=eventSwitch;
}
}
cell.textLabel.text=[settingArr objectAtIndex:indexPath.row];
cell.textLabel.font=[UIFont fontWithName:@"HelveticaNeue-Light" size:16];
return cell;
}
该开关也可以添加到附件视图中,只需在情节提要中添加UITableView,而无需在其中拖动UITableViewCell(不添加UITableViewCell)。这样,在上面的代码中,如果(cell==nil)将调用条件。下面的代码适用于我,将accesoryView作为UISwitch
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
if(indexPath.row==3){
UISwitch *eventSwitch=[[UISwitch alloc] initWithFrame:CGRectZero];
[eventSwitch addTarget:self action:@selector(toggleEventNotification:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView=eventSwitch;
}
}
cell.textLabel.text=[settingArr objectAtIndex:indexPath.row];
cell.textLabel.font=[UIFont fontWithName:@"HelveticaNeue-Light" size:16];
return cell;
}
[cell.contentView将子视图带到前面:switchBtn];@Muhammaddnan我尝试了你的代码,但它仍然没有显示。它没有在屏幕外?如果你使用与标签相同的坐标会发生什么?还要尝试删除cell.accessoryView=switchBtn;
并将其添加到cell.contentView中。由于accessoryView将其显示在右侧,因此框架似乎更大了一点移动“[cell addSubview:switchBtn]`并使用initWithFrame:CGRectMake(200,10,150,50)
代替initWithFrame:CGRectZERO
@Sunkas感谢它的工作。[cell.contentView带来subview-tofront:switchBtn];@Muhammaddnan我尝试了你的代码,但它仍然没有显示。它没有在屏幕外?如果你使用与标签相同的坐标会发生什么?还要尝试删除cell.accessoryView=switchBtn;
并将其添加到cell.contentView中。由于accessoryView将其显示在右侧,因此框架似乎更大了一点移动“[cell addSubview:switchBtn]使用initWithFrame:CGRectMake(200,10,150,50)
代替initWithFrame:CGRectZERO
@Sunkas谢谢它工作了。如果我使用“cell.accessoryView=switchBtn;”它不会显示,但如果我使用[cell.contentView addSubview:switchBtn];它会显示..如果我使用“cell.accessoryView=switchBtn;”它不显示,但如果我使用[cell.contentView addSubview:switchBtn];它将显示。。