UISwitch 在 UITableview 中不可见(iOS 8)?
UISwitch is not visible in UITableview(iOS 8)?
我在 UITableview 中创建了一个 UISwitch,但它在我 project.Can 中引用和实施的确切 row.Code 中不可见,任何人都可以告诉我如何更正它。
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");
}
}
你的很多代码都没有什么意义。例如,当您的 switchButton 尚不可见时,[switchBtn setOn:YES animated:YES];
毫无意义(也就是说,尚未插入视图层次结构,不会出现动画。
接下来,您声明一个没有值的变量row
,然后将开关的标签赋值给它??结果可能是未定义的,而且它肯定不会为您带来任何有意义的东西。如果您只需要 indexPath
?
行,那么这一切有什么意义呢?
接下来,您将 switchButton 分配给辅助视图,然后将其添加为单元格的子视图!两者之一当然是不必要的。可能应该保留附件视图以获得真正的 'accessory' 功能(阅读 UITableView doc)。
但对我来说最大的问题是你如何处理单元重用,而你的代码对此并不清楚。您必须知道单元格是重复使用的([tableView dequeueReusableCell...
方法)。为一行创建的单元格稍后可以由另一行中的 table 视图实例重复使用。一个常见的错误是为单元格创建特定内容,期望它停留在它最初出现的位置。
在该 UITableView 委托方法中,您必须为当前正在考虑的行设置 所有 特定单元格的属性。也就是说,还要确保 switchButton 不 出现在其他行的单元格中。
很难解释,但你应该post整个方法的实现,以便我们有一个更好的想法。
参考这段代码
- (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;
switch也可以添加到accessory view中,只需要在storyboard中添加UITableView,不用拖拽UITableViewCell进去(不要添加UITableViewCell)。这样在上面的代码中条件if(cell==nil)就会be called.The 下面的代码适用于我使用 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 中创建了一个 UISwitch,但它在我 project.Can 中引用和实施的确切 row.Code 中不可见,任何人都可以告诉我如何更正它。
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");
}
}
你的很多代码都没有什么意义。例如,当您的 switchButton 尚不可见时,[switchBtn setOn:YES animated:YES];
毫无意义(也就是说,尚未插入视图层次结构,不会出现动画。
接下来,您声明一个没有值的变量row
,然后将开关的标签赋值给它??结果可能是未定义的,而且它肯定不会为您带来任何有意义的东西。如果您只需要 indexPath
?
接下来,您将 switchButton 分配给辅助视图,然后将其添加为单元格的子视图!两者之一当然是不必要的。可能应该保留附件视图以获得真正的 'accessory' 功能(阅读 UITableView doc)。
但对我来说最大的问题是你如何处理单元重用,而你的代码对此并不清楚。您必须知道单元格是重复使用的([tableView dequeueReusableCell...
方法)。为一行创建的单元格稍后可以由另一行中的 table 视图实例重复使用。一个常见的错误是为单元格创建特定内容,期望它停留在它最初出现的位置。
在该 UITableView 委托方法中,您必须为当前正在考虑的行设置 所有 特定单元格的属性。也就是说,还要确保 switchButton 不 出现在其他行的单元格中。
很难解释,但你应该post整个方法的实现,以便我们有一个更好的想法。
参考这段代码
- (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;
switch也可以添加到accessory view中,只需要在storyboard中添加UITableView,不用拖拽UITableViewCell进去(不要添加UITableViewCell)。这样在上面的代码中条件if(cell==nil)就会be called.The 下面的代码适用于我使用 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;
}