我有一個看似隨機的問題,其中我的UITableView
中的某些單元格顯示默認情況下應該隱藏的數據。Swift UITableViewCell顯示默認情況下隱藏的數據
解釋隱藏的數據。
這是一個股票投資組合,您可以點擊一個單元格以展開它(並查看此數據)。當它關閉時,數據再次隱藏起來。
下面是我正面臨的問題的屏幕截圖,導致隨機單元默認顯示此數據。
在上面的截圖它的「RUS.TO」而這顯示隱藏的數據中的「CUB」細胞。
以下是如何其是應該被隱藏的意見和標籤設置:
// #Advanced data
stockNameView.setTranslatesAutoresizingMaskIntoConstraints(false)
stockNameView.hidden = true
self.addSubview(stockNameView)
purchasePriceView.setTranslatesAutoresizingMaskIntoConstraints(false)
purchasePriceView.hidden = true
self.addSubview(purchasePriceView)
lastPriceView.setTranslatesAutoresizingMaskIntoConstraints(false)
lastPriceView.hidden = true
self.addSubview(lastPriceView)
daysHeldView.setTranslatesAutoresizingMaskIntoConstraints(false)
daysHeldView.hidden = true
self.addSubview(daysHeldView)
8級的標籤,真實重疊是上述4次的子視圖。
正如你所看到的,這4個視圖默認設置爲隱藏。所以我不認爲問題在於我的單元班?
在我的代碼中只有一個地方,我將這4個UIViews的隱藏值設置爲false。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("com.Stocks.portfolioCell", forIndexPath: indexPath) as! portfolioCell
if indexPath.row % 2 == 0 {
cell.backgroundColor = UIColor.whiteColor()
} else {
cell.backgroundColor = UIColor.formulaWhiteColor()
}
if selectedCellIndexPath == indexPath {
cell.tickerLabel.textColor = UIColor.formulaBlueColor()
cell.heightSeperator.hidden = false
cell.stockNameView.hidden = false
cell.purchasePriceView.hidden = false
cell.lastPriceView.hidden = false
cell.daysHeldView.hidden = false
cell.endSeperator.hidden = false
tableHeight.constant = tableHeight.constant+250
self.scrollView.contentSize = CGSize(width:self.view.bounds.width, height: self.contentView.frame.height)
}
if lastCollapsing == true || currentCollapsing == true {
cell.tickerLabel.textColor = UIColor.formulaDarkGrayColor()
cell.heightSeperator.hidden = true
cell.stockNameView.hidden = true
cell.purchasePriceView.hidden = true
cell.lastPriceView.hidden = true
cell.daysHeldView.hidden = true
cell.endSeperator.hidden = true
lastCollapsing = false
currentCollapsing = false
tableHeight.constant = tableHeight.constant-250
self.scrollView.contentSize = CGSize(width:self.view.bounds.width, height: self.contentView.frame.height)
}
if stocks.count > 0 {
let formulaStock = stocks[indexPath.row]
ticker = formulaStock.valueForKey("ticker") as! String!
let fontAwesomeBlueAttribute = [NSForegroundColorAttributeName: UIColor.formulaBlueColor(), NSFontAttributeName: UIFont(name: "FontAwesome", size: 12)!]
let fontAwesomeGreenAttribute = [NSForegroundColorAttributeName: UIColor.formulaGreenColor(), NSFontAttributeName: UIFont(name: "FontAwesome", size: 12)!]
let latoBoldDarkGrayAttribute = [NSForegroundColorAttributeName: UIColor.formulaDarkGrayColor(), NSFontAttributeName: UIFont(name: "Lato-Bold", size: 12)!]
if ticker != "CASH" {
let tickerString = " \(ticker)" as NSString
var tickerAttributedString = NSMutableAttributedString(string: tickerString as String)
tickerAttributedString.addAttributes(fontAwesomeBlueAttribute, range: tickerString.rangeOfString(""))
tickerAttributedString.addAttributes(latoBoldDarkGrayAttribute, range: tickerString.rangeOfString(" \(ticker)"))
cell.tickerLabel.attributedText = tickerAttributedString
} else {
let tickerString = " \(ticker)" as NSString
var tickerAttributedString = NSMutableAttributedString(string: tickerString as String)
tickerAttributedString.addAttributes(fontAwesomeGreenAttribute, range: tickerString.rangeOfString(""))
tickerAttributedString.addAttributes(latoBoldDarkGrayAttribute, range: tickerString.rangeOfString(" \(ticker)"))
cell.tickerLabel.attributedText = tickerAttributedString
}
weight = formulaStock.valueForKey("weight") as! Float
cell.weightLabel.text = "\(weight.roundTo(2))%"
cell.filledWeightWidth.constant = CGFloat(weight/2)
lastPrice = formulaStock.valueForKey("lastPrice") as! Float
purchasePrice = formulaStock.valueForKey("purchasePrice") as! Float
percentDifference = ((lastPrice/purchasePrice)*100.00)-100
if ticker == "CASH" {
cell.changeLabel.text = ""
} else if percentDifference > 0 {
cell.changeLabel.text = ("+\(percentDifference.roundTo(2))%")
cell.changeLabel.textColor = UIColor.formulaGreenColor()
} else if percentDifference < 0 && percentDifference > -100 {
cell.changeLabel.text = ("\(percentDifference.roundTo(2))%")
cell.changeLabel.textColor = UIColor.formulaRedColor()
} else if percentDifference == 0 {
cell.changeLabel.text = ("\(percentDifference.roundTo(2))%")
cell.changeLabel.textColor = UIColor.formulaGreenColor()
} else {
cell.changeLabel.text = "N/A"
cell.changeLabel.textColor = UIColor.formulaDarkGrayColor()
}
cell.stockNameLabel.text = formulaStock.valueForKey("name") as! String!
cell.purchasePriceLabel.text = "$\(purchasePrice)"
cell.lastPriceLabel.text = "$\(lastPrice)"
daysHeld = formulaStock.valueForKey("daysHeld") as! Int
cell.daysHeldLabel.text = "\(daysHeld)"
}
cell.selectionStyle = UITableViewCellSelectionStyle.None
return cell
}
但是將單元格的隱藏值設置爲false的代碼段。需要我的selectedCellIndexPath
等於cellForRowAtIndexPath
正在經歷的當前indexPath。
此處,我設置selectedCellIndexPath
:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.dequeueReusableCellWithIdentifier("com.Stocks.portfolioCell", forIndexPath: indexPath) as! portfolioCell
if let selectedCellIndexPath = selectedCellIndexPath {
if selectedCellIndexPath == indexPath {
self.selectedCellIndexPath = nil
currentCollapsing = true
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
} else {
lastCollapsing = true
self.selectedCellIndexPath = indexPath
tableView.reloadRowsAtIndexPaths([lastIndexPath], withRowAnimation: .None)
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
self.lastIndexPath = indexPath
}
} else {
selectedCellIndexPath = indexPath
self.lastIndexPath = indexPath
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
}
tableView.beginUpdates()
tableView.endUpdates()
}
哪些應該永遠只能是什麼,當我在電池接頭。
我每次用新的數據集提供的UITableView我也一定要運行下面的代碼行:
(menuTabBarController.viewControllers as! [Portfolio_ViewController])[1].selectedCellIndexPath = nil
這臺selectedCellIndexPath
至零,以避免任何潛在的問題。
那麼,爲什麼有些隨機單元顯示默認情況下應該隱藏的信息?
當我測試這個bug時,我沒有敲擊任何單元格。因此,selectedCellIndex應該永遠不會有值。
我有simillar問題,因爲我是作爲subViews編程添加標籤,但從不刪除它們。而不是添加和隱藏它們,嘗試添加它們,當您想要顯示它們時,並刪除它們,當你想隱藏它們時。 –