簡體   English   中英

內部子類化的UITableViewCell重寫Func LayoutSubviews被稱為Double

[英]Inside Subclassed UITableViewCell Override Func LayoutSubviews Being Called Double

我有一個UITableViewCell的子類,該子類具有對每個單元override func layoutSubviews()兩次的override func layoutSubviews() 這將在單元中創建元素的多個副本。

UITableView返回正確的單元格計數並顯示正確的單元格數量,但是layout函數會將少數幾個屬性重置為nil。 因此,錯誤地渲染了很多數據。

TableView里面的UIViewController:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return userMatches.count    // return 2 correctly
}


func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1

}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {


    return self.view.bounds.height * 0.20

}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell: MatchTableViewCell = self.matchTableView.dequeueReusableCellWithIdentifier("matchCell", forIndexPath: indexPath) as! MatchTableViewCell

    cell.matchDetails = userMatches[indexPath.row]
    cell.userInteractionEnabled = true
    cell.selectionStyle = UITableViewCellSelectionStyle.None

    return cell // creates 2 correctly

}

UITableView子類:

   override func layoutSubviews() {
    super.layoutSubviews()

// runs 4 times

    let userHelperIconArray = [userZoom, userTakeTwo, userStopper]
    let opponentHelperIconArray = [opponentZoom, opponentTakeTwo, opponentStopper]

    layoutHelperInventoryIcons(self, opponentHelperIconArray: opponentHelperIconArray, userHelperIconArray: userHelperIconArray, opponentNameLabel: opponentName)
    layoutMiniGameboard(self)

    self.layer.borderWidth = 1.0
    self.layer.borderColor = UIColor.blackColor().CGColor

    print("one")

    turnIdentifier(self, matchDetails: matchDetails, opponentNameLabel: opponentName)

}

無論如何,您都不應該在layoutSubviews執行此操作。 而是將配置移至單獨的方法,並讓您的表格視圖數據源在出隊單元格后調用該配置方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM