V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
tsinghan
V2EX  ›  iDev

在 UICollectionView 的 cell 中添加 subView 不显示的问题

  •  
  •   tsinghan · 2015-01-29 21:42:36 +08:00 · 8082 次点击
    这是一个创建于 3375 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在cellForItemAtIndexPath方法中

    var videoView = videos["video"] as UIView
    videoView.frame = cell.frame
    self.collectionView.addSubview(videoView)
    

    如果我设置了 videoView.frame = cell.frame 这句,那么这个subview 就不会显示

    我修改了frame(包含在cell中)

    var videoView = videos["video"] as UIView
    videoView.frame = videoView.frame = CGRect(x: 37, y: 94, width: 100, height: 100)
    self.collectionView.addSubview(videoView)
    

    这样就显示了

    请问这是什么原因?

    9 条回复    2015-01-29 23:44:41 +08:00
    krafttuc
        1
    krafttuc  
       2015-01-29 22:36:49 +08:00
    没看明白楼主为什么要在 collection view 里添加这些个 video view,而不是把他们放到 cell 里。
    keithellis
        2
    keithellis  
       2015-01-29 22:39:17 +08:00
    自定义一个 UICollectionViewCell,在 layoutSubviews 方法中设置 frame,另外 subView 我一般放在 contentView 里。
    mailworks
        3
    mailworks  
       2015-01-29 22:57:13 +08:00
    如楼上,自定义的View添加到UICollectionViewCell的contentView 中,
    videoView.frame = cell.bounds这样设置
    ruandao
        4
    ruandao  
       2015-01-29 23:04:44 +08:00
    把frame 打印出来~~
    tsinghan
        5
    tsinghan  
    OP
       2015-01-29 23:11:53 +08:00
    @krafttuc
    @keithellis 抱歉我贴错代码了 我是放到 contentView里的 cell.contentView.addSubview(videoView)
    tsinghan
        6
    tsinghan  
    OP
       2015-01-29 23:12:24 +08:00
    @mailworks 恩 我试试这种方法
    tsinghan
        7
    tsinghan  
    OP
       2015-01-29 23:26:51 +08:00
    @krafttuc
    @keithellis
    @ruandao
    @mailworks 恩 用videoView.frame = cell.bounds 这种方式设置确实可以显示了
    我分别打印了 cell.bounds 和 cell.frame 分别为 (0.0,0.0,155.0,125.0) 和 (0.0,230.0,155.0,125.0)
    而我cell的大小就是 (0.0,230.0,155.0,125.0) 这样的, 为什么要videoView.frame = cell.bounds 设置?没搞懂
    vincentxue
        8
    vincentxue  
       2015-01-29 23:32:20 +08:00
    我靠 我刚发现我写的回复怎么没出来。。。。。
    重新贴一下吧还好我有剪贴板历史记录。

    如果你一定要在这里添加子视图,那坐标应该这么写:

    videoView.frame = cell.bounds;

    如果是 tableView 要在 willDisplayCell 中设置 frame,因为 UITableViewCell 从初始化到显示之前都不知道自己的 frame 的,默认都是 CGRectZero。

    但自定义 cell 通常应该子类化。

    frame 是相对于父视图而言的,

    假设有一个 UICollectionView, frame 为 {{0, 0}, {320, 480}};
    假设 cell 大小是 {40, 40},间隙为 0, 0 的话,那么你屏幕上显示的第一行(8 个)的 cell 的 frame 应该如下:

    0: {{0, 0}, {40, 40}}
    1: {{40, 0}, {40, 40}}
    2: {{80, 0}, {40, 40}}
    3: {{120, 0}, {40, 40}}
    4: {{160, 0}, {40, 40}}
    5: {{200, 0}, {40, 40}}
    6: {{240, 0}, {40, 40}}
    7: {{280, 0}, {40, 40}}

    你可以想象一下,如果你的父视图才 {40, 40},你自身的 frame.origin.x 或者 y 就超过 40 了,肯定是看不见的了。
    tsinghan
        9
    tsinghan  
    OP
       2015-01-29 23:44:41 +08:00
    @vincentxue 明白了 十分感谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2801 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 06:17 · PVG 14:17 · LAX 23:17 · JFK 02:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.