Unique value of object to use in section

二次信任 提交于 2019-12-25 17:06:23

问题


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.

I have the following code:

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 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

}

it doesn't work.

Where am I wrong?


回答1:


Filtering the movies by genre each time the table view is reloaded is quite expensive.

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:

• 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]
  }
}

Now insert the movies

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
}

In cellForRowAtIndexPath populate the cells with

...
   let genre = unique[indexPath.section]
   let movie = loadMovie[genre]![indexPath.row]
   cell.textLabel!.text = movie.name
...

An alternative is an array of arrays




回答2:


An update for Swift 4

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return unique[section]
    }

func numberOfSections(in tableView: UITableView) -> Int {
    return unique.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let genre = unique[section]
     return loadMovie[genre]!.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "basicCell", for: indexPath)
    let genre = unique[indexPath.section]
    let movie = loadMovie[genre]![indexPath.row]
    cell.textLabel!.text = movie.name
    return cell
}


来源:https://stackoverflow.com/questions/35457371/unique-value-of-object-to-use-in-section

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!