gpt4 book ai didi

swift - 自定义 UITableViewCell 的测试,cellForRowAtIndexPath 因零导出而崩溃

转载 作者:行者123 更新时间:2023-11-28 11:17:26 25 4
gpt4 key购买 nike

我有一个包含 tableView 的 ViewController。由于我需要用测试很好地覆盖代码,因此我需要为 [tableView:cellForRowAtIndexPath] 编写测试

import UIKit

class MainViewController: UIViewController, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!

var source = [
Country(name: "USA", capital: "Washington, D.C."),
Country(name: "Argentina", capital: "Buenos Aires"),
Country(name: "Mexico", capital: "Mexico, D.F.")
]
let cellIdentifier = NSStringFromClass(MainViewController.self)

init() {
super.init(nibName: "MainViewController", bundle: nil)
}

required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
super.viewDidLoad()

let nib = UINib(nibName: "CustomCell", bundle: nil)
tableView?.registerNib(nib, forCellReuseIdentifier: cellIdentifier)
tableView.dataSource = self
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return source.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? CustomCell
if let cell = cell {
cell.countryLabel.text = source[indexPath.row].name
cell.capitalLabel.text = source[indexPath.row].capital
return cell
}
return CustomCell()
}
}

struct Country {
var name: String
var capital: String
}

测试如下所示:

import UIKit
import XCTest

class MainViewControllerTests: XCTestCase {

var controller: MainViewController!

override func setUp() {
super.setUp()

controller = MainViewController()
controller.view.description
}

override func tearDown() {
super.tearDown()
}

func testTableViewOutlet() {
XCTAssertNotNil(controller.tableView)
}

func testTableViewCellForRowAtIndexPath() {
let indexPath = NSIndexPath(forRow: 0, inSection: 0)

let cell = controller.tableView(controller.tableView, cellForRowAtIndexPath: indexPath) as! CustomCell

XCTAssertEqual(cell.countryLabel.text!, "USA")
XCTAssertEqual(cell.capitalLabel.text!, "Washington, D.C.")
}
}

运行测试时出现以下错误:

Test Case '-[CustomTableViewCellTests.MainViewControllerTests testTableViewCellForRowAtIndexPath]' started.
fatal error: unexpectedly found nil while unwrapping an Optional value

由于 [dequeueReusableCellWithIdentifier] 仅在从测试运行时才返回 nil,因此此函数已在我的生产应用程序中暗示了几个月的测试。实际上,我已经尝试过许多在 StackOverflow 中阅读过的不同方法,例如:

  1. 在 [dequeueReusableCellWithIdentifier] 返回 nil 时创建 CustomCell
  2. 使用临时 Controller 为 CustomCell 加载 NIB 并获取应该连接到单元格的 View

底线是我接近解决方案,我创建了一个 CustomCell 对象,但单元格的导出仍然为零。我还没有找到将导出实现为初始化元素的代码片段,以便我可以通过测试。

我包括一个基本的 iOS 项目,它有自己的 git,其中包含我在此处包含的代码。它是一个包含 UITableView 的 UIViewController,UITableView 使用自定义 UITableViewCell 显示数据。我希望找到一个解决方案,实现自定义电池及其导出,使测试通过。

Git repo 源: https://bitbucket.org/jallauca/customcell/src

克隆 git 仓库:

git clone https://bitbucket.org/jallauca/customcell.git 

最佳答案

对我来说,(Swift 3)我不得不调用 cellForRow 数据源 cellForRowAt 函数,而不是直接从 tableView.cellForRow 函数访问

//this 
let cell = controller.tableView(controller.tableView, cellForRowAt: IndexPath(row: 0, section: 0))

//instead of this
let cell = controller.tableView.cellForRow(at: IndexPath(row: 0, section: 0))

关于swift - 自定义 UITableViewCell 的测试,cellForRowAtIndexPath 因零导出而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30832276/

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