【问题标题】:UIView resize inside a UITableViewCell not displaying unless table is scrolled除非滚动表格,否则 UITableViewCell 内的 UIView 调整大小不显示
【发布时间】:2014-05-15 03:02:07
【问题描述】:

我在UITableViewCell(动态原型,不确定是否需要澄清这一点)和background color 中有一个UIView,并使用以下代码(在cellForRowAtIndexPath 方法内)更改了view's frame

UIView* verde = (UIView*) [cell viewWithTag:202];
verde.frame =CGRectMake(20, 30, x, y);

问题是当UITableView 第一次绘制时(当屏幕加载时)UIView 具有原始大小(默认情况下在情节提要的原始原型上建立)。但是当我向下滚动时,单元格离开屏幕,因此被另一个单元格重用。滚动回单元格时,再次调用cellForRowAtIndexPath,现在框架的大小正确。

我在更改框架后尝试调用[verde setNeedsDisplay];,但没有成功。

【问题讨论】:

  • 你在使用自动布局吗?
  • 你能把cellForRowAtIndexPath代码放上去吗?
  • 禁用自动布局解决了这个问题,谢谢!

标签: ios objective-c uitableview uiview


【解决方案1】:

禁用自动布局解决了 Timothy Moose 指出的问题。不知何故,第一次绘制(屏幕首次加载)中的单元格保留了情节提要中指定的布局,当它们离开屏幕并被重用或再次创建时,最终会使用正确的框架绘制视图。

【讨论】:

  • 还有其他不需要禁用自动布局的解决方案吗?
  • 如何以编程方式禁用自动布局?
  • 禁用自动布局解决了我的问题(由首次创建 UIView 时自动布局干扰引起),也许在创建视图后操作框架也可以解决问题,但禁用自动布局完成了工作@Oren
  • 我从未以编程方式禁用自动布局,仅在 XCode @Unome 中
  • 好吧,你可以用谷歌搜索一下,几秒钟后就可以找到了……不管怎样,看看this link@Unome
【解决方案2】:

试试这个,希望你能解决问题

-(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;

}

【讨论】:

  • 感谢您的调试想法,这是一个很好的想法。禁用自动布局解决了这个问题。
猜你喜欢
  • 2012-09-03
  • 1970-01-01
  • 2014-12-03
  • 1970-01-01
  • 1970-01-01
  • 2017-08-30
  • 2021-12-27
  • 2021-07-24
  • 1970-01-01
相关资源
最近更新 更多