在tableview中滚动时,UISwitch关闭状态不稳定

时间:2017-10-09 08:50:06

标签: ios objective-c uitableview uiswitch

我在其中一个tableview数据源函数(索引行的单元格)中以编程方式创建UISwitch。当我滚动表视图时,OFF状态开关出现故障(UI)出现了两轮。附上交换机的屏幕截图。enter image description here

感谢您的帮助!

let query = new Parse.Query('company');
user.get('vz5tF2g4Pi')
 .then(u => {
   //query.
   return query.find('users', u)
})
.then(rs => console.log('.... ', rs))
.catch(err => console.log('err ', err))

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,并在创建开关

之前修复了以下代码
for(UIView *subView in cell. contentView.subviews){
        if ([subView isKindOfClass:[UISwitch class]]) {
        [subView removeFromSuperview];

    }
    }

static NSString *TableIdentifier = @"YourCellIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableIdentifier];

if (cell == nil) {
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableIdentifier];
    //place your all UI elements code here.
  }

答案 1 :(得分:0)

我已经更新了你的代码,试试这个会对你有用,

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    UILabel *title = (UILabel*)[cell viewWithTag:80];
    UILabel *description = (UILabel*)[cell viewWithTag:81];
    UIView *innerView = (UIView *)[cell viewWithTag:82];

    innerView.layer.borderColor=[[UIColor colorWithRed:229/255.0f green:229/255.0f blue:229/255.0f alpha:1.0]  CGColor];
    innerView.layer.borderWidth = 2.0f;
    NSDictionary *displayDict = scenarioListsArray[indexPath.row];
    title.text =[displayDict objectForKey:@"name"];
    description.text = [displayDict objectForKey:@"description"];    
    UISwitch *myswitch = [cell.contentView viewWithTag:indexPath.row+597];
    if(mySwitch == nil){
        myswitch = [[UISwitch alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width-60,(cell.contentView.frame.size.height/2)-20 , 100, 100)];
       [cell.contentView addSubview:myswitch];
       [myswitch addTarget:self action:@selector(cellButtonClickAction:) forControlEvents:UIControlEventValueChanged];

    }
    myswitch.onTintColor = [UIColor colorWithRed:25/255.0f green:122/255.0f blue:66/255.0f alpha:1];
    myswitch.tag = indexPath.row+597; //597 is extra number added to tag
    if ([[displayDict objectForKey:@"status"] isEqualToString:@"ACTIVE"]) {
        [myswitch setOn:YES animated:YES];
    }
    else
    {
        [myswitch setOn:NO animated:YES];
    }

    return cell;
}
相关问题