在 tableView 中显示数组

Posted

技术标签:

【中文标题】在 tableView 中显示数组【英文标题】:Displaying array in tableView 【发布时间】:2019-01-11 18:16:51 【问题描述】:

目前正在开发一个应用程序,该应用程序具有现有数组中所有项目的列表(位于单独的 .swift 文件中)。这是由 tableView 完成的。但是,我每次只得到一个空的 tableview。知道出了什么问题吗?

import UIKit
import MapKit

//Initialize the TableViewController
class CategoriesController: UIViewController, UITableViewDataSource, UITableViewDelegate 

//Retrieve the array
var locationsAll = [Location]()

//Outlet
@IBOutlet var tableView: UITableView!

//Load view
override func viewDidLoad() 
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self


func numberOfSectionsInTableView(tableView: UITableView) -> Int 
    return 1


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    return locationsAll.count


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    let cell = tableView.dequeueReusableCell(withIdentifier: "allCell") ?? UITableViewCell(style: .subtitle, reuseIdentifier: "allCell")

    let location = locationsAll[indexPath.row]
    cell.textLabel?.text = location.title
    cell.detailTextLabel?.text = location.rating

    return cell
     

至于数组,我使用了结构体。结构体也会在 MKMapView 上被调用。

struct Location 
    let title: String
    let rating: String
    let description: String
    let latitude: Double
    let longitude: Double

包含结构和数据的 .swift 文件:

struct Location 
        let title: String
        let rating: String
        let description: String
        let latitude: Double
        let longitude: Double
    

let locations = [
    Location(title: "something", rating: "", description: "Old.", latitude: 10.11111, longitude: 1.11111),
        Location(title: "something", rating: "", description: "Old.", latitude: 10.11111, longitude: 1.11111),
        Location(title: "something", rating: "", description: "Old.", latitude: 10.11111, longitude: 1.11111)
]

我在文件中使用var locationsAll = [Location]() 调用它 提前谢谢!

【问题讨论】:

你永远不会向你的数组中添加任何元素locationsAll 在单独的 .swift 文件中 ?? @Joakim Danielson 你到底是什么意思?该数组已经填充了我希望通过 tableView 显示的数据。 @Sh_Khan 数组,称为 Location。因为有几个控制器需要数组内的数据。 那个数组看起来不像填充了数据,你能告诉我们你是如何以及在哪里填充数据的吗? 【参考方案1】:

那么你需要

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    return locations.count


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    let cell = tableView.dequeueReusableCell(withIdentifier: "allCell") ?? UITableViewCell(style: .subtitle, reuseIdentifier: "allCell")

    let location = locations[indexPath.row]
    cell.textLabel?.text = location.title
    cell.detailTextLabel?.text = location.rating

    return cell
     

看起来let locations = [ 是一个全局变量,因此它可以在任何地方访问,或者您可以声明

var locationsAll = [Location]()

然后在viewDidLoad

locationsAll  = locations 

【讨论】:

成功了。我怎么会错过添加样式...非常非常感谢!【参考方案2】:

不鼓励使用全局数组作为数据源。数据应该封装在结构、类或枚举中。

您可以在控制器内创建位置。

替换

var locationsAll = [Location]()

var locationsAll = [
    Location(title: "something", rating: "", description: "Old.", latitude: 10.11111, longitude: 1.11111),
    Location(title: "something", rating: "", description: "Old.", latitude: 10.11111, longitude: 1.11111),
    Location(title: "something", rating: "", description: "Old.", latitude: 10.11111, longitude: 1.11111)
]

或者将位置声明为结构中的静态变量

struct Location 
    let title: String
    let rating: String
    let description: String
    let latitude: Double
    let longitude: Double

    static let locations = [
        Location(title: "something", rating: "", description: "Old.", latitude: 10.11111, longitude: 1.11111),
        Location(title: "something", rating: "", description: "Old.", latitude: 10.11111, longitude: 1.11111),
        Location(title: "something", rating: "", description: "Old.", latitude: 10.11111, longitude: 1.11111)
    ]

并填充数据源数组

var locationsAll = Location.locations

【讨论】:

确实更好。我会在代码中调整它。谢谢! 我现在有一个问题,我使用for location in locations 循环向我的mapView 添加注释,但这给出了一个我无法解决的错误。 locations 是一个常数。将信息添加到控制器中locationsAll中的对象中。 谢谢一百万!代码现在更好了。【参考方案3】:

计数将等于 0。这就是不渲染单元格的原因。添加数据后,您需要在表视图上重新设置数据。

我假设您在故事板中设置单元格?否则,您需要在使用前注册单元格。

【讨论】:

是的,我确实是通过情节提要完成的。给它字幕属性等。也是标识符。计数怎么会是0? 没有向数组添加数据的代码。或者你在它运行后添加它,你需要在 ui 线程上调度重新加载! 谢谢@msphn!

以上是关于在 tableView 中显示数组的主要内容,如果未能解决你的问题,请参考以下文章

使用自动布局将视图定位在 tableview 的顶部

滚动时tableview数据重复

在 ios 中不包含 Tableview 的情况下创建 UIPopOver

当我将 tableview 添加到 ViewController 时,tableview 未正确添加

如何仅在表格视图的文本最后一部分中添加页脚文本?

UIRefreshControl 导致不正确的 TableView 偏移量