- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
为了总结我的问题,我正在尝试测试 Collection View 的数据源。我初始化一个 View Controller 并调用 viewDidLoad()
来初始化组件,该组件初始化自定义类以用作数据源。这是我正在测试的类(class)。
程序在此处开始单元测试:
class BubbleViewManagerTest: XCTestCase {
var viewController : DashboardViewController = DashboardViewController()
//var bubbleViewManager : BubbleViewManager = BubbleViewManager()
override func setUp() {
super.setUp()
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle(forClass: self.dynamicType))
viewController = storyboard.instantiateViewControllerWithIdentifier("DashboardViewControllerId") as! DashboardViewController
bubbleViewManager = viewController.bubbleViewManager
viewController.loadView()
viewController.viewDidLoad()
}
func testCellForItemAtIndexPath() {
DataManager.singleton.dayActivities = []
DataManager.singleton.addGoal(Period(name: "asdf", hour: 1, minute: 1, color: UIColor.blackColor(), priority: PeriodPriority.Low))
var cell = bubbleViewManager.collectionView(viewController.bubbleView, cellForItemAtIndexPath: NSIndexPath(forRow: 0, inSection: 0)) as! BubbleCollectionViewCell
XCTAssertEqual(cell.bounds.width, 45)
XCTAssertEqual(cell.bounds.height, 45)
XCTAssertNotNil(cell.bubbleView.period)
DataManager.singleton.addGoal(Period(name: "asdf", hour: 1, minute: 1, color: UIColor.blackColor(), priority: PeriodPriority.Medium))
DataManager.singleton.addGoal(Period(name: "asdf", hour: 1, minute: 1, color: UIColor.blackColor(), priority: PeriodPriority.High))
var index = NSIndexPath(forRow: 1, inSection: 0)
cell = bubbleViewManager.collectionView(viewController.bubbleView, cellForItemAtIndexPath: index) as! BubbleCollectionViewCell
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let period: Period = DataManager.singleton.dayActivities[indexPath.row]
var cell: BubbleCollectionViewCell = BubbleCollectionViewCell()
switch period.priority {
case .High:
cell = collectionView.dequeueReusableCellWithReuseIdentifier(self.reuseIdentifierHighPriority, forIndexPath: indexPath) as! BubbleCollectionViewCell
break;
case .Medium:
cell = collectionView.dequeueReusableCellWithReuseIdentifier(self.reuseIdentifierMediumPriority, forIndexPath: indexPath) as! BubbleCollectionViewCell
break;
case .Low:
cell = collectionView.dequeueReusableCellWithReuseIdentifier(self.reuseIdentifierLowPriority, forIndexPath: indexPath) as! BubbleCollectionViewCell
}
// Give the bubble view the period for it to work on.
cell.bubbleView.period = period
// The bubble view needs to monitor a timer to redraw the bubble. Observer is assigned here.
if let timer = cell.bubbleView.period?.globalTimer {
timer.observer = cell.bubbleView
}
return cell
}
程序在 cell = collectionView.dequeueReusableCellWithReuseIdentifier(self.reuseIdentifierMediumPriority, forIndexPath: indexPath) 作为行中断! BubbleCollectionViewCell
我知道当一个对象在自动释放后被访问时或者当它一开始没有被初始化时,就会发生 EXC_BAD_ACCESS。在我的例子中,我得到了错误并确保所有对象都已初始化并且它们的引用是强引用(而不是弱引用)。我启用了 NSZombies,但没有成功。
令我困惑的是 func collectionView(collectionView:cellForItemAtIndexPath:) -> UICollectionViewCell
在线程中断之前已经执行了一次。
这可能与我在测试目标上运行有关吗?当我在 iOS 模拟器上运行该应用程序时,此代码按预期运行。
如有任何帮助,我们将不胜感激。
最佳答案
这是通过在添加新数据进行测试后调用 reloadData()
解决的。尝试模拟 View Controller 来测试数据源时。必须调用 viewController.collectionView.reloadData()
。我从未考虑过这一点,因为它是在 viewDidLoad()
调用中完成的,而且是在添加数据之前完成的。
关于ios - UICollectionView 单元测试 dequeueReusableCellWithReuseIdentifier 上的错误访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31062681/
这个问题在这里已经有了答案: UICollectionView registerClass: forCellWithReuseIdentifier method breaks UICollectio
我刚刚开始使用 Collection View 。这是一个空的 Collection View 单元格,尚未添加任何内容。我只是想先看看它的外观,然后再添加更多 UI 元素。 但是注意到滚动并不顺畅。
当我在模拟器中运行我的应用程序时,我遇到了这个运行时错误。 Terminating app due to uncaught exception 'NSInternalInconsistencyExce
我有一个 UICollectionView,我正在使用 dequeueReusableCellWithReuseIdentifier 来重用单元格。第二次调用 cellForItemAtIndexPa
我一直想知道为什么我的代码在获取 Collection View 单元格时与 cellForItemAtIndexPath: 一起工作,而不与 dequeueReusableCellWithReuse
我使用 UICollectionView 和自定义 collectionViewCells,并且我确切地知道 dequeueReusableCellWithReuseIdentifier 的工作原理。
我不能像使用 UITableView 那样任意出列单元格来计算高度。使用 UICollectionView 执行此操作的等效方法是什么? dequeueResuableCellWithReuseIde
我创建了一个 UICollectionViewCell 的子类 with a .xib,并且在 - (CategoryViewCollectionCell *)collectionView:(UICo
我有一个包含自定义 UICollectionViewCell 的 UICollectionView(TestReceiptCell 是类名)。 当自定义单元格仅包含 UILabel 时,让 UICol
为了总结我的问题,我正在尝试测试 Collection View 的数据源。我初始化一个 View Controller 并调用 viewDidLoad() 来初始化组件,该组件初始化自定义类以用作数
我在 UICollectionViewCell 中的 UICollectionView 遇到了这个问题:最后一个 UICollectionViewCell 没有填充 13 - 18,而是复制了第一个。
将点击手势有效添加到 UICollectionViewCell 的 subview 的最佳方法是什么?从 dequeueReusableCellWithReuseIdentifier 返回已经附加了一
我们遇到了崩溃,我们只能从崩溃日志中看到这些: 2018-11-02 10:15:01.744674-0400 Flights[385:17563] *** Terminating app due t
为 UICollectionView 创建单元格时, dequeueReusableCellWithReuseIdentifier不通过 init也不initWithCoder CategoryVie
我正在开发一个有 UICollectionView 的应用程序。我创建了一个包含 UIImageView 和 UILabel 的自定义 Collection View Cell。以下代码导致 cell
我遇到以下崩溃: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'coul
登录方式: - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndex
在 collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayo
在 UICollectionViewCell 类中有几个关于 nil outlet 的帖子,例如 this和 this ,但没有一个解决方案有效。使用强导出而不是弱导出失败,registerClass
在我的项目中,我有多种类型的 UITableview 单元格,每个单元格都包含 UICollectionview。 最初我在 UITableview 的 2 个部分加载了 2 个 tableViewc
我是一名优秀的程序员,十分优秀!