我在UIView
(动态原型,不确定是否重要的是澄清这一点)中使用UITableViewCell
获得了background color
并使用以下代码更改了view's frame
(在cellForRowAtIndexPath
方法内):
UIView* verde = (UIView*) [cell viewWithTag:202];
verde.frame =CGRectMake(20, 30, x, y);
问题在于,当第一次绘制UITableView
时(屏幕加载时)UIView
具有原始大小(默认情况下在故事板中的原始原型上建立)。但是当我向下滚动时,细胞离开屏幕,因此被另一个细胞重复使用。当滚动回单元格时,再次调用cellForRowAtIndexPath
,现在框架的大小正确。
我在尝试更改框架后尝试调用[verde setNeedsDisplay];
但没有成功。
答案 0 :(得分:2)
禁用自动布局解决了Timothy Moose指出的问题。不知何故,第一次绘制(屏幕第一次加载)中的单元格保留了故事板中指定的布局,当它们离开屏幕并重新使用或再次创建时,最终使用正确的框架绘制视图。
答案 1 :(得分:0)
试试这个,希望您能解决问题
-(UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
else{
[[cell.contentView viewWithTag: 202] removeFromSuperview];
}
UIView *view =[[UIView alloc]init];
view.frame = cell.contentView.frame;
view.tag = 202;
view.backgroundColor = [UIColor redColor];//To be sure that the custom view in the cell
[cell addSubview:view];
return cell;
}