我正試圖用較短的方式編寫下面的代碼。你如何製作一個函數來設置其他實例的屬性?
func colourChangeIfDeletionCancelled(word:Int){
for i in 0...selectedWords[word].count - 1 {
let square = selectedWords[word][i]
arrayOfRows[square.0][square.1].topToRight.backgroundColor = nil
arrayOfRows[square.0][square.1].topToLeft.backgroundColor = nil
arrayOfRows[square.0][square.1].bottomToRight.backgroundColor = nil
arrayOfRows[square.0][square.1].bottomToLeft.backgroundColor = nil
arrayOfRows[square.0][square.1].horizontalTube.backgroundColor = nil
arrayOfRows[square.0][square.1].verticalTube.backgroundColor = nil
arrayOfRows[square.0][square.1].endFromLeft.backgroundColor = nil
arrayOfRows[square.0][square.1].endFromRight.backgroundColor = nil
arrayOfRows[square.0][square.1].endFromTop.backgroundColor = nil
arrayOfRows[square.0][square.1].endFromBottom.backgroundColor = nil
}
}
此代碼有效,但我確信有一個更好(更短)的方法來編寫它,但我不確定如何。我試圖做一個函數,將子視圖作爲一個變量,但在如何做到這一點上丟失了。我相信這不是最難的問題,但我堅持它,任何幫助表示讚賞。
編輯:
arrayOfRows就是一個類的數組的數組我創建了一個叫做LetterSquareView
class LetterSquareView: UIView {
var letter:String!
@IBOutlet weak var topToLeft: UIView!
@IBOutlet weak var topToRight: UIView!
@IBOutlet weak var bottomToRight: UIView!
@IBOutlet weak var bottomToLeft: UIView!
@IBOutlet weak var horizontalTube: UIView!
@IBOutlet weak var verticalTube: UIView!
@IBOutlet weak var endFromLeft: UIView!
@IBOutlet weak var endFromRight: UIView!
@IBOutlet weak var endFromTop: UIView!
@IBOutlet weak var endFromBottom: UIView!
@IBOutlet weak var topToLeftWhiteSpace: UIView!
@IBOutlet weak var topToRightWhiteSpace: UIView!
@IBOutlet weak var bottomToRightWhiteSpace: UIView!
@IBOutlet weak var bottomToLeftWhiteSpace: UIView!
@IBOutlet weak var letterSquareViewView: LetterSquareViewView!
@IBOutlet var letterSquareView: UIView!
@IBOutlet weak var letterLbl: UILabel!
init(frame: CGRect, letter: String) {
super.init(frame: frame)
NSBundle.mainBundle().loadNibNamed("LetterSquareView", owner: self, options: nil)
letterLbl.text = letter.capitalizedString
self.letter = letter
self.addSubview(letterSquareView)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
,我試圖改變背景顏色有背景色設置時,子視圖他們被創建。我試圖刪除它們時刪除該顏色。我認爲會有一個功能,你可以輸入一個視圖的數組,並輸入一個屬性來設置,它會被設置,但我找不到任何類型的東西。
什麼是'arrayOfRows',什麼是'square'? 'arrayOfRows'中元素的類型是什麼? – Alexander
您希望我們幫助您提高數據結構的使用率,而無需向我們提供有關數據結構的任何信息。那該怎麼辦? – Alexander
您是否包含arrayOfRows的每個子視圖?或特定類別的每個子視圖。 – Sethmr