gpt4 book ai didi

ios - Swift UITableViewCell 显示默认隐藏的数据

转载 作者:搜寻专家 更新时间:2023-10-31 22:35:15 25 4
gpt4 key购买 nike

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

解释隐藏的数据。

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

这是我遇到的导致随机单元格默认显示此数据的问题的屏幕截图。

enter image description here

在上面的屏幕截图中,显示隐藏数据的是“RUS.TO”和“CUB”单元格。

下面是应该隐藏的 View 和标签的设置方式:

// #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 个 View 的 subview 。

如您所见,这 4 个 View 默认设置为隐藏。所以我认为问题不在于我的单元格类?

我的代码中只有一处将这 4 个 UIView 的隐藏值设置为 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
}

但是将单元格的隐藏值设置为假的代码部分。要求我的 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 设置为 nil,以避免任何潜在问题。

那么,为什么一些随机单元格会显示本应默认隐藏的信息?

当我测试这个错误时,我没有点击任何单元格。所以 selectedCellIndex 此时甚至不应该有值。

最佳答案

这很可能是由单元格重用引起的 - 如果以前选择的单元格用作未选择的单元格,则其 subview 的隐藏值不会改变。

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

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

关于ios - Swift UITableViewCell 显示默认隐藏的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30524662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com