我有一个看似随机的问题,其中我的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编程添加标签,但从不删除它们。而不是添加和隐藏它们,尝试添加它们,当您想要显示它们时,并删除它们,当你想隐藏它们时。 –