简体   繁体   English

如何将 Set 与 Table View 一起使用?

[英]How do you use Set with Table View?

I want to iterate through Set and display them in a table, but I'm getting an error that says:我想遍历Set并将它们显示在一个表中,但我收到一条错误消息:

Fatal error: Invalid index致命错误:无效索引

Following is my code:以下是我的代码:

@NSManaged public var progress: Set<Progress>

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    cell.textLabel?.text = progress[progress.index(progress.startIndex, offsetBy: indexPath.row)].comment
    return cell
}

A Set is unordered.一个Set是无序的。 Sort the set – for example by date – to get an array in a specific order and use this as data source array对集合进行Sort (例如按date )以按特定顺序获取数组并将其用作数据源数组

var progresses = [Progress]() 

...   

progresses = progress.sorted{$0.date < $1.date}

Simply use indexPath.row as index in the Array after converting the Set<Progress> to Array<Progress> using the Array(Set) initializer.使用Array(Set)初始化程序将Set<Progress>转换为Array<Progress>后,只需使用indexPath.row作为Array中的索引。

cell.textLabel?.text = Array(progress)[indexPath.row].comment

Better Approach: Using the Array(Set) initializer in cellForRowAt each time is less efficient, also the order of elements cannot be guaranteed.更好的方法:每次在cellForRowAt中使用Array(Set)初始化程序效率较低,也无法保证元素的顺序。 It would be better to do it just once right after the progress: Set<Progress> is initialized.最好在progress: Set<Progress>已初始化。

var progressArray: [Progress] // set this right after `progress` is set

Then in cellForRowAt :然后在cellForRowAt

cell.textLabel?.text = progressArray[indexPath.row].comment

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您如何为表格视图单元格制作和使用 XIB 文件,而不是仅在故事板的表格视图中使用单元格? - How do you make and use an XIB file for a table view cell, rather than just using a cell "in" the table view in storyboard? 在ios 6中,如何在View控制器中使用“容器视图”对象和嵌入在其中的表视图控制器之间使用传递数据? - In ios 6, how do you use pass data between View controller holding 'Container View' object and Table View controller that is embedded in it? Xcode:如何保存可编辑的 Object 数组并使用它来填充表格视图? - Xcode: How do you save an editable Object Array and use it to populate a table view? 如何将接口构建器中的UITabBarContoller设置为根视图控制器? - How do you set UITabBarContoller in interface builder as root view controller? 如何将UI元素放入表格视图 - How do you put UI elements inside table view 如何设置表格视图的编辑附件的背景色? - How do I set the background color of the editing accessory of a table view? 如何为下面的表格视图设置正确的约束? - How do I set the correct constraints for the table view below? 在使用 SQLite3 插入值后关闭视图控制器后,如何刷新表视图? - How do you refresh a table view once dismissing a View Controller after inserting values using SQLite3? 如何设置UILayoutPriority? - How do you set UILayoutPriority? 从ViewController向View传递数据时如何设置委托? - How do you set the delegate when passing data from ViewController to View?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM