Как создать собственный вид коллекции программно?

В моем проекте я хочу проектировать пользовательский View Cell программно с помощью SVGKImageView & UILabel, как это

здесь квадрат - SVGKImageView, а "Dadar" - UILabel.

Но теперь я получаю вывод, как это

Это мой код

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
 {
    // intialize collection cell with media collection cell
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("mediaCell", forIndexPath: indexPath) as! ViewContentMediaCollectionCell

    // get the data from view model
    let viewModelobj = mViewContentViewModelObj!.getViewContentData()

    // set the page imageview frame with default size
    var mSVGImageView = SVGKFastImageView(frame: CGRect(x: 0, y: 0, width: cell.frame.width, height: (50)))

    // set the pageImageview with actual pageImage
    mSVGImageView = SVGKFastImageView(SVGKImage: viewModelobj[indexPath.row].mContentPage)

    // add page imageview in the cell
    cell.contentView.addSubview(mSVGImageView)

    // add the title label
    let titleLabel : UILabel = UILabel(frame: CGRect(x: 0, y: cell.frame.height - 20, width: cell.frame.width, height: 20))

    // set the page title
    titleLabel.text = viewModelobj[indexPath.row].mPageTitle

    // add title label to the collection view cell
    cell.contentView.addSubview(titleLabel)

    // return the cell
    return cell

  }

1 ответ

Вы должны создать собственное представление, которое содержит ImageView и метку, подобную вашему дизайну, а затем добавить к contentView.

Другие вопросы по тегам