如何设置UICollectionviewCell

时间:2016-12-09 06:54:42

标签: swift uicollectionviewcell

我在故事板中的集合视图单元格中添加了一个图像。我想将图像设置为四舍五入。但是,它不起作用。

ViewController

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ContentCollectionViewCell
    return cell
}

ContentCollectionViewCell

class ContentCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var userProfileImageView: UIImageView!

    required init?(coder aDecoder: NSCoder) {
        // called
        super.init(coder: aDecoder)
    }


    override init(frame: CGRect) {
        // never called
        super.init(frame: frame)
        setRoundedImage()
    }

    func setRoundedImage() {
        userProfileImageView.layer.cornerRadius = userProfileImageView.frame.width * 0.5
    }

}

但它在 ViewController 中以这种方式工作。

为什么永远不会调用 ContentCollectionViewCell 中的init(frame: CGRect)?如果我想在UICollectionCell类中设置预定义的内容,最佳做法是什么?

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ContentCollectionViewCell
    cell.userProfileImageView.layer.cornerRadius = cell.userProfileImageView.frame.width * 0.5
    return cell
}

4 个答案:

答案 0 :(得分:4)

尝试使用此代码我的帮助。

绕着牢房。 //舍入整个单元格

    cell.contentView.layer.cornerRadius = 2.0;
    cell.contentView.layer.borderWidth = 1.0;
    cell.contentView.layer.borderColor = UIColor.clearColor().CGColor;
    cell.contentView.layer.masksToBounds = true;

OR

在单元格类中添加以下内容//单元格的圆形图像视图

class ExploreCategoryCollectionViewCell: UICollectionViewCell
{

@IBOutlet var coverImage: UIImageView!
@IBOutlet var lblCoverName: UILabel!
@IBOutlet var lblArtistName: UILabel!
@IBOutlet var lblTags: UILabel!
@IBOutlet var lblE: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()

    self.coverImage.layer.cornerRadius = 5.0
    self.coverImage.layer.masksToBounds = true
}

答案 1 :(得分:2)

在单元格类中,您可以对单元格进行任何初始设置:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="favicon.ico">
    <title><%= @title %></title>
    <%= csrf_meta_tags %>

    <%= stylesheet_link_tag "application", :media => "all" %>

    <!-- For third-generation iPad with high-resolution Retina display: -->
    <!-- Size should be 144 x 144 pixels -->
    <%= favicon_link_tag 'apple-touch-icon-144x144-precomposed.png', :rel => 'apple-touch-icon-precomposed', :type => 'image/png', :sizes => '144x144' %>

    <!-- For iPhone with high-resolution Retina display: -->
    <!-- Size should be 114 x 114 pixels -->
    <%= favicon_link_tag 'apple-touch-icon-114x114-precomposed.png', :rel => 'apple-touch-icon-precomposed', :type => 'image/png', :sizes => '114x114' %>

    <!-- For first- and second-generation iPad: -->
    <!-- Size should be 72 x 72 pixels -->
    <%= favicon_link_tag 'apple-touch-icon-72x72-precomposed.png', :rel => 'apple-touch-icon-precomposed', :type => 'image/png', :sizes => '72x72' %>

    <!-- For non-Retina iPhone, iPod Touch, and Android 2.1+ devices: -->
    <!-- Size should be 57 x 57 pixels -->
    <%= favicon_link_tag 'apple-touch-icon-precomposed.png', :rel => 'apple-touch-icon-precomposed', :type => 'image/png' %>

    <!-- For all other devices -->
    <!-- Size should be 32 x 32 pixels -->
    <%= favicon_link_tag 'favicon.ico', :rel => 'shortcut icon' %>

    <%= javascript_include_tag "application" %>

    <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
    <!--[if lt IE 9]>
    <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.2/html5shiv.min.js" type="text/javascript"></script>
    <![endif]-->
  </head>
  <body>
    <div id="navbar" class="navbar navbar-default">
      <div class="container-fluid">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <div class="navbar-brand">
          <%=link_to image_tag("knowuros-icon.gif", :alt=>"Brand", :class=>"Brand_Image visible-md visible-lg"), home_index_url %>
          <span><%= link_to "Know Your Operating System", home_index_url, :id => "Brand_Text" %><small>One solution to all of your software problems</small></span>
        </div>
        <div class="navbar-collapse collapse navbar-responsive-collapse">
          <ul class="nav navbar-nav">
            <% Category.roots.each do |category| %>
              <li class="<%= is_active?('')><%= link_to category.name, category, :class => "link_text" %></li>
            <% end %>
            <li class="<%= is_active?('contacts','new') %>"><%= link_to " ContactUs", new_contact_url, :class =>'glyphicon glyphicon-user' %></li>
          </ul>
          <ul class="nav navbar-nav navbar-right">
            <% if auth_user_signed_in? %>
              <li class="<%= is_active?('articles,'new') %>"><%= link_to " NewArticle", new_article_url, :class =>'glyphicon glyphicon-new-window' %></li>
              <li><%= link_to " SignOut", destroy_auth_user_session_url, :method => 'delete', :class => "glyphicon glyphicon-log-out" %></li>
            <% else %>
              <li class="<%= is_active?('auth_users','sign_in') %>"><%= link_to " Login", new_auth_user_session_url, :class => "glyphicon glyphicon-log-in"  %></li>
              <li class="<%= is_active?('auth_users,'sign_up') %>"><%= link_to " Register", new_auth_user_registration_url, :class => "glyphicon glyphicon-registration-mark" %></li>
            <% end %>
            <form id= "search" class="navbar-form">
              <div class="form-group">
                <input type="text" class="form-control" placeholder="Search">
              </div>
              <button type="submit" class="btn btn-default glyphicon glyphicon-search">Search</button>
            </form>
          </ul>
        </div>
      </div>
    </div>
    <div class="container">
      <div class="row">
        <%= bootstrap_flash %>
        <%= yield %>
      </div><!--/row-->
      <footer>
        <p>&copy; Company 2016</p>
      </footer>
    </div> <!-- /container -->
  </body>
</html>

答案 2 :(得分:0)

您必须为所有单元格设置它。将此代码写入cellForItemAt

cell.userProfileImageView.layer.cornerRadius = cell.userProfileImageView.frame.width * 0.5
cell.userProfileImageView!.clipsToBounds = true

希望它能起作用...... !!

答案 3 :(得分:0)

您应该在

中添加舍入代码行
layoutSubviews()

不在init()