在单元测试中获取UITableView中的数字行

编程入门 行业动态 更新时间:2024-10-24 06:26:01
单元测试中获取UITableView中的数字行 - swift?(Get number rows in UITableView while unit tests- swift?)

我正在为UITableView编写UIViewController的测试用例。 我想问如何获取UITableView中的行数

func testloadingDataIntoUiTableView() { var countRow:Int = viewController.formListTableView.numberOfRowsInSection XCTAssert(countRow == 4) }

I'm writing a test case for UIViewController that has UITableView. I want to ask how can I get number of rows in UITableView

func testloadingDataIntoUiTableView() { var countRow:Int = viewController.formListTableView.numberOfRowsInSection XCTAssert(countRow == 4) }

最满意答案

介绍

请记住,数据模型会生成UI。 但是你不应该查询UI来检索你的数据模型(除非我们正在讨论用户输入)。

让我们看看这个例子

class Controller:UITableViewController { let animals = ["Tiger", "Leopard", "Snow Leopard", "Lion", "Mountain Lion"] let places = ["Maveriks", "Yosemite", "El Capitan"]; override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 2 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { switch section { case 0: return animals.count case 1: return places.count default: fatalError() } } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { guard let cell = tableView.dequeueReusableCellWithIdentifier("MyCellID") else { fatalError("Who took the MyCellID cell???") } switch indexPath.section { case 0: cell.textLabel?.text = animals[indexPath.row] case 1: cell.textLabel?.text = places[indexPath.row] default: fatalError() } return cell } }

丑陋的解决方案

在这种情况下,要获取表中的总行数,我们应该查询模型( animals和places属性),如此

let controller: Controller = ... let rows = controller.animals.count + controller.places.count

好的解决方案

或者甚至更好,我们可以使animals和places属性保密,并添加像这样的计算属性

class Controller:UITableViewController { private let animals = ["Tiger", "Leopard", "Snow Leopard", "Lion", "Mountain Lion"] private let places = ["Maveriks", "Yosemite", "El Capitan"]; var totalNumberOfRows: Int { return animals.count + places.count } ...

现在你可以使用它

let controller: Controller = ... let rows = controller.totalNumberOfRows

Introduction

Please keep in mind that the data model generates the UI. But you should not query the UI to retrieve your data model (unless we are talking about user input).

Lets look at this example

class Controller:UITableViewController { let animals = ["Tiger", "Leopard", "Snow Leopard", "Lion", "Mountain Lion"] let places = ["Maveriks", "Yosemite", "El Capitan"]; override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 2 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { switch section { case 0: return animals.count case 1: return places.count default: fatalError() } } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { guard let cell = tableView.dequeueReusableCellWithIdentifier("MyCellID") else { fatalError("Who took the MyCellID cell???") } switch indexPath.section { case 0: cell.textLabel?.text = animals[indexPath.row] case 1: cell.textLabel?.text = places[indexPath.row] default: fatalError() } return cell } }

The ugly solution

In this case to get the total number of rows into the table we should query the model (the animals and places properties), so

let controller: Controller = ... let rows = controller.animals.count + controller.places.count

The nice solution

Or even better we could make the animals and places properties private and add a computed property like this

class Controller:UITableViewController { private let animals = ["Tiger", "Leopard", "Snow Leopard", "Lion", "Mountain Lion"] private let places = ["Maveriks", "Yosemite", "El Capitan"]; var totalNumberOfRows: Int { return animals.count + places.count } ...

Now you can use this

let controller: Controller = ... let rows = controller.totalNumberOfRows

更多推荐

本文发布于:2023-08-05 18:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1435729.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元   数字   测试中   UITableView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!