2015-05-29 217 views
2

我有一个看似随机的问题,其中我的UITableView中的某些单元格显示默认情况下应该隐藏的数据。Swift UITableViewCell显示默认情况下隐藏的数据

解释隐藏的数据。

这是一个股票投资组合,您可以点击一个单元格以展开它(并查看此数据)。当它关闭时,数据再次隐藏起来。

下面是我正面临的问题的屏幕截图,导致随机单元默认显示此数据。

enter image description here

在上面的截图它的“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应该永远不会有值。

+0

我有simillar问题,因为我是作为subViews编程添加标签,但从不删除它们。而不是添加和隐藏它们,尝试添加它们,当您想要显示它们时,并删除它们,当你想隐藏它们时。 –

回答

1

当您重复使用单元格时,旧值仍然存在,因此每次使用dequeueReusableCellWithIdentifier时,都需要重置默认值或仍然缓存在其中的最后一个值。

From apple documentation:

表视图的数据源实现 的tableView的:的cellForRowAtIndexPath:应始终重置所有内容时 重用细胞。

+0

这个伎俩,非常感谢你! –

2

这很可能是由于单元重用造成的 - 如果先前选择的单元格被用作未选中的单元格,则hidden的子视图值不会更改。

您可以通过添加else分支解决这个问题:

if selectedCellIndexPath == indexPath { 
    ... 
} else { 
    // reset cell to non-selected state 
    ... 
} 
+3

UITableViewCells也有一个'prepareForReuse'方法,可以用来重置任何状态 – chedabob

+0

没有尝试过这个解决方案,因为其他答案少了2行,但是我认为这样做也是一样。 –

+0

@chedabob我试过cell.prepareForReuse,但没有解决我的问题。 –

0

dequeueReusableCell获得细胞后,做到这一点。从视图中清除垃圾数据的负载,并且它也提供更好的性能。

let cell = userTable.dequeueReusableCell(withIdentifier: "users", for: indexPath) 


for v in cell.subviews { 
    v.removeFromSuperview() 
} 
相关问题