要在节中使用的对象的唯一值 [英] Unique value of object to use in section
问题描述
我有一个类,我需要获取流派对象,放入一个数组中,获取唯一值(因为我会有很多)并使用该数组填充 tableView 的部分.
I have a Class and I need to get genre object, put in an array, get unique value (because I will have many) and use the array to populate section of a tableView.
我有以下代码:
class Movie {
var name: String!
var plot: String!
var imageUrl: String!
var genre: String!
init(name: String, plot: String, img: String, genre: String) {
self.name = name
self.plot = plot
self.imageUrl = img
self.genre = genre
}
}
}
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
var loadMovie = [Movie]()
let insertMovie = Movie(name: "Blow", plot: "Blow bla bla bla", img: "", genre: "Action")
let insertMovie2 = Movie(name: "BLade", plot: "Blade bla bla bla", img: "", genre: "Action")
let insertMovie3 = Movie(name: "Inside Out", plot: "Blow bla bla bla", img: "", genre: "Kids")
let insertMovie4 = Movie(name: "Titanic", plot: "Blow bla bla bla", img: "", genre: "Drama")
loadMovie.append(insertMovie)
loadMovie.append(insertMovie2)
loadMovie.append(insertMovie3)
loadMovie.append(insertMovie4)
let unique = NSSet(array: loadMovie.map { $0.genre })
unique 返回唯一值(Action、Drama、Kids),但是当我尝试获取部分时:
unique returns the unique value ( Action, Drama, Kids), but when I try to get section:
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return unique[section].genre
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return unique.count
}
它不起作用.
我哪里错了?
推荐答案
每次重新加载表视图时按流派过滤电影的成本非常高.
Filtering the movies by genre each time the table view is reloaded is quite expensive.
这是一个使用标题(流派)名称数组和包含每个流派的 [Movie]
数组的字典的解决方案.
This is a solution using an array of header (genre) names and a dictionary containing a [Movie]
array for each genre respectively.
var unique = [String]()
var loadMovie = [String:[Movie]]()
添加插入电影的方法.逻辑是:
Add a method to insert the movies. The logic is:
• 如果该类型存在,则将该类型的电影添加到字典中的 [Movie]
数组中.
• 如果流派不存在,请将流派添加到 unique
数组并将电影添加到字典中.
• If the genre exists, add the movie for that genre to the [Movie]
array in the dictionary.
• If the genre does not exist, add the genre to the unique
array and the movie to the dictionary.
func insertMovie(movie : Movie) {
let genre = movie.genre
if unique.contains(genre) {
loadMovie[genre]!.append(movie)
} else {
unique.append(genre)
loadMovie[genre] = [movie]
}
}
现在插入电影
let movie = Movie(name: "Blow", plot: "Blow bla bla bla", img: "", genre: "Action")
insertMovie(movie)
let movie2 = Movie(name: "BLade", plot: "Blade bla bla bla", img: "", genre: "Action")
insertMovie(movie2)
let movie3 = Movie(name: "Inside Out", plot: "Blow bla bla bla", img: "", genre: "Kids")
insertMovie(movie3)
let movie4 = Movie(name: "Titanic", plot: "Blow bla bla bla", img: "", genre: "Drama")
insertMovie(movie4)
相关的数据源和委托方法是
The relevant data source and delegate methods are
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return unique[section]
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return unique.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let genre = unique[section]
return loadMovie[genre]!.count
}
在 cellForRowAtIndexPath
中填充单元格
...
let genre = unique[indexPath.section]
let movie = loadMovie[genre]![indexPath.row]
cell.textLabel!.text = movie.name
...
另一种方法是数组数组
这篇关于要在节中使用的对象的唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!