我正在研究斯坦福Swift作业->这里是链接: https://drive.google.com/drive/folders/1-TWZDChwwzkiGzt78QlPZDzN-j82JfR6
当前,当用户将更多纸牌添加到“表视图”时,我将更多“纸牌视图”作为子视图添加到“表视图”。效果很好,但是不会将新的子视图设置为相同大小。每次添加子视图时,我都会更新grid.cellCount,这会更改卡的大小。但是以前的子视图保持不变,并且不会更新。每次创建新卡时,我都试图调用setNeedsLayout(),但它不起作用。
我希望表格视图可以布局其所有子视图,以便它们都具有相同的大小。这是我得到的视图,这是代码。
这是我想要的视图,其中每次将新卡添加到表视图时,所有卡都更新为相同大小。
这是我的表格视图的代码。
import UIKit
class TableView: UIView {
//Initializes the Grid File
lazy var grid = Grid(layout: Grid.Layout.aspectRatio(0.63), frame:
bounds)
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
public func createCard(shape : String, color : String, count : Int, fill
: String, index: Int) {
//Update the cellCount in grid so it resizes each subview
grid.cellCount = index + 1
setNeedsLayout()//Not Working
//Init and add a new PlayingCard Subview to the Table View
let card = PlayingCardView(frame: grid[index]!.insetBy(dx: 5, dy:
5), shape: shape, count: count, color: color, fill: fill)
self.addSubview(card)
}
override func draw(_ rect: CGRect) {
//Draw the table on the main view
let tableGrid = UIBezierPath(roundedRect: grid.frame, cornerRadius:
8)
UIColor.green.setFill()
tableGrid.fill()
}
}
这是我的ViewController的函数,在其中调用TableView
func createCards(indexes : [Int]) {
Table.setNeedsLayout()//Tried calling setNeedsLayout(). Not working
for index in indexes {
var card = deck.draw()//Draw a card from the model file
card?.identifier = index
deck.tableCards.append(card!)
//call the create card function on the Table View
Table.createCard(shape: (card?.symbol.rawValue)!, color: (card?.color.rawValue)!, count: (card?.count.rawValue)!, fill: (card?.fill.rawValue)!, index: index)
}
}