我需要在完全加载UITableView后调用一个函数.我知道在大多数情况下,第一次加载视图时不会显示每一行,但在我的情况下,它确实是因为我总共只有8行.
令人讨厌的部分是被调用的函数需要访问一些tableView数据,因此,我无法在表完全加载之前调用它,否则我会得到一堆错误.
在viewDidAppear中调用我的函数会损害用户体验,因为此函数会更改UI.把它放在viewWillAppear搞砸执行(我不知道为什么)并将它放在viewDidLayoutSubviews中工作得非常好但是每次布局更改时都会调用它我害怕在重新加载时会出现一些错误的tableView.
我找不到关于这个话题的帮助.试过我在这里找到的一些东西,但不幸的是它没有用,因为它看起来有点过时了.可能重复的帖子的解决方案不起作用,我在发布之前尝试过.
任何帮助表示赞赏!谢谢.
编辑:我正在用一些数据填充我的tableView,我没有遇到任何问题.我有2个部分,每4行.默认情况下,用户只能看到5行(第一部分中有4行,而第二部分中只有一行,其余部分则被隐藏).当用户单击第一部分的第一行时,它将显示第二部分的第一行.当他点击第一部分的第二行时,它会显示第二部分的两行,依此类推.如果用户再次单击第一部分的第一行,则仅显示第二部分中的一个单元格.然后他可以保存他的选择.
同时,系统会更改第一部分中所选行的颜色,以便用户知道该怎么做.
我的部分问题是我想在我的数据库中更新模型.如果用户想要修改记录,那么我需要将存储在我的数据库中的值与ViewController相关联.因此,例如,如果他当时选择了选项2,我需要确保第一部分中的第二行具有不同的颜色,并且当他尝试访问视图时显示第二部分中的两行.
这是一些代码:
func setNonSelectedCellColor(indexPath: NSIndexPath) { let currentCell = tableView.cellForRowAtIndexPath(indexPath) currentCell?.textLabel?.textColor = UIColor.tintColor() for var nbr = 0; nbr <= 3; nbr++ { let aCell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: nbr,inSection: 0)) let aCellIndexPath = tableView.indexPathForCell(aCell!) if aCellIndexPath?.row != indexPath.row { aCell?.textLabel?.textColor = UIColor.blackColor() } } } func hideAndDisplayPriseCell(numberToDisplay: Int,hideStartIndex: Int) { for var x = 1; x < numberToDisplay; x++ { let priseCell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: x,inSection: 1)) priseCell?.hidden = false } if hideStartIndex != 0 { for var y = hideStartIndex; y <= 3; y++ { let yCell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: y,inSection: 1)) yCell?.hidden = true } } }
override func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) { let path = (indexPath.section,indexPath.row) switch path { case(0,0): setNonSelectedCellColor(indexPath) hideAndDisplayPriseCell(1,hideStartIndex: 1) data["frequencyType"] = Medecine.Frequency.OneTime.rawValue case(0,1): setNonSelectedCellColor(indexPath) hideAndDisplayPriseCell(2,hideStartIndex: 2) data["frequencyType"] = Medecine.Frequency.TwoTime.rawValue case(0,2): setNonSelectedCellColor(indexPath) hideAndDisplayPriseCell(3,hideStartIndex: 3) data["frequencyType"] = Medecine.Frequency.ThreeTime.rawValue case(0,3): setNonSelectedCellColor(indexPath) hideAndDisplayPriseCell(4,hideStartIndex: 0) data["frequencyType"] = Medecine.Frequency.FourTime.rawValue default:break } }
我将值存储在字典中,以便在保存时可以处理验证.
我想在tableView完成加载后立即调用前两个函数.例如,我初始化第一行时不能要求数据源显示/隐藏1行或更多行,因为尚未创建这些行.
正如我所说,如果在viewDidAppear中调用这些函数,它几乎可以正常工作,因为它不会立即选择行,也不会尽快在第二部分中显示相应的行数.我必须在它之前等待1-2秒.