ios - 单元格表格视图中的视图位置仅在滚动后更改

ios - view position inside cell tableview changes only after scrolling

我有一个使用 ios 8.0+ 的 tablewiew,我在每个单元格中添加了一个复选框 - 在 iphone 5 上效果很好,但是在 iphone 6 上测试时 - 复选框是只有在滚动后才在正确的位置...... (第二张是滚动后的)

**** 还不能添加照片:) 所以我在第一次加载时实际看到的是复选框有一些右填充 -> 在我滚动一些复选框后移动到右边 - 它们应该在的位置 下面的代码 谢谢!

CGFloat y =self.navigationController.navigationBar.frame.size.height + self.navigationController.navigationBar.frame.origin.y;
tableData = [[UITableView alloc] initWithFrame:CGRectMake(5, y+10, self.view.frame.size.width, self.view.frame.size.height - y -30 - nextButton.frame.size.height -10) style:UITableViewStylePlain];
tableData.dataSource = self;
tableData.delegate = self;



[self.view addSubview:tableData];

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

  }
   else
    {
        CTCheckbox *check =  [cell.subviews objectAtIndex:1];
        [check removeFromSuperview];
    }






CTCheckbox *checkbox;

cell.contentView.backgroundColor = [UIColor grayColor];
checkbox = [[CTCheckbox alloc] initWithFrame:CGRectMake(cell.frame.size.width-60, cell.frame.size.height/2-10, 50, 20.0)];
checkbox.alpha = 0.7;
[checkbox addTarget:self action:@selector(checkboxDidChange:) forControlEvents:UIControlEventValueChanged];
checkbox.tag = indexPath.row;
[checkbox setColor:[BlendedColors pink] forControlState:UIControlStateNormal];

//Expertise label
UIFont *myFont = [UIFont systemFontOfSize:12.0 ];
cell.textLabel.font  = myFont;

cell.textLabel.text = [experties objectAtIndex:indexPath.row];
cell.textLabel.textColor = [BlendedColors black];
[cell addSubview:checkbox];
cell.selectionStyle = UITableViewCellStyleDefault;


if([[selectedCheckBox objectAtIndex:indexPath.row]isEqualToString:@"YES"])
{
    checkbox.checked = YES;
}
else{
    checkbox.checked = NO;
}


//cell.contentView.backgroundColor = [UIColor blackColor];
return cell;

}

已修复 - 第一个单元格宽度始终为 320

添加了 3 行

if (cell == nil) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    //Fix Bug - first cell view is always 320 - now its dynamic(cell width = device width)
        CGRect cellFrame = cell.frame;
        cellFrame.size.width = self.view.frame.size.width;
        cell.frame = cellFrame;


  }