简体   繁体   English

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

[英]ios - view position inside cell tableview changes only after scrolling

I have a tablewiew using ios 8.0+, i added a checkbox to each cell - on iphone 5 works great, but when testing on iphone 6 - the checkbox is in the right position only after scrolling... (the second picture is after scrolling) 我有一个使用ios 8.0+的tablewiew,我在每个单元格上都添加了一个复选框-在iPhone 5上效果很好,但是在iPhone 6上进行测试时-该复选框只有在滚动后才处于正确位置...(第二张图片是在滚动后)

****cant add photos yet:) so what i actually see at first load is that the checkbox get some rightpadding -> after I scroll some of the checkboxes moves to the right - where they should be Code below Thank you! ****还不能添加照片:)因此,我第一次加载时实际看到的是该复选框得到了一些右移->在我将某些复选框向右滚动后,它们应该在下面的代码中谢谢了!

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;

} }

Fixed - First cell width is always 320 固定-第一个像元宽度始终为320

added 3 lines 增加了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;


  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM