简体   繁体   English

如何在Swift 2中填充自定义UIView?

[英]How to populate Custom UIView in swift 2?

I am new to swift and iOS development, and its quite confusing! 我是Swift和iOS开发的新手,这很令人困惑! I come from web development environment using php, mysql and html (a smarter environment ;) ) 我来自使用php,mysql和html的Web开发环境(更智能的环境;))

My goal here is to list data in cool design not typical table view, so I am using a custom class "CardView" that I found on the internet: 我的目标是在很酷的设计中列出数据,而不是典型的表视图,因此我使用的是在网上找到的自定义类“ CardView”:

import UIKit

@IBDesignable
public class CardView: UIView {

@IBInspectable var cornerRadius: CGFloat = 2

@IBInspectable var shadowOffsetWidth: Int = 0
@IBInspectable var shadowOffsetHeight: Int = 3
@IBInspectable var shadowColor: UIColor? = UIColor.blackColor()
@IBInspectable var shadowOpacity: Float = 0.5

override public func layoutSubviews() {
    layer.cornerRadius = cornerRadius
    let shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius)

    layer.masksToBounds = false
    layer.shadowColor = shadowColor?.CGColor
    layer.shadowOffset = CGSize(width: shadowOffsetWidth, height: shadowOffsetHeight);
    layer.shadowOpacity = shadowOpacity
    layer.shadowPath = shadowPath.CGPath
}

}

I added the CardView as framework, played around with it in storyboard. 我添加了CardView作为框架,并在情节提要中对其进行了使用。 I made a card with my preferences of shadow and border-radius, now I want to get serious and render each data set in a card, and list all cards vertically. 我用阴影和边框半径的偏好制作了一张卡片,现在我想变得严肃起来,渲染卡片中的每个数据集,并垂直列出所有卡片。

在此处输入图片说明

I included the CardView class to my ViewController, now what? 我将CardView类包含到ViewController中,现在呢? :) How to begin I am confused.. I know I need a class to take array of data, then loop through the array, and print each data inside a CardVeiw object. :)如何开始我很困惑。我知道我需要一个类来获取数据数组,然后遍历该数组,并在CardVeiw对象中打印每个数据。

I am just not sure how to make that! 我只是不确定如何做到这一点! totally new mac user, and new to xcode. 全新的Mac用户,并且是xcode的新用户。

Your next steps are: 下一步是:

  1. Change your UIView to a UITableViewCell 将您的UIView更改为UITableViewCell

  2. Add everything necessary for a UITableViewCell Class 添加UITableViewCell类所需的所有内容

  3. Add a Tableview to your ViewController and load the CustomTableViewCells in this TableView 将Tableview添加到ViewController并在此TableView中加载CustomTableViewCells

For everything above there are a lot tutorials out there. 对于上面的所有内容,这里都有很多教程。 I hope this helps. 我希望这有帮助。

There are no limits. 没有限制。 You won't get a normal tableview if you use your own CustomCells. 如果您使用自己的CustomCell,则不会获得普通的表格视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM