gpt4 book ai didi

ios - UICollectionView 单元测试 dequeueReusableCellWithReuseIdentifier 上的错误访问

转载 作者:搜寻专家 更新时间:2023-10-30 23:10:43 24 4
gpt4 key购买 nike

为了总结我的问题,我正在尝试测试 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/

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