У меня есть база данных Firebase, структурированная так:
![Screen Shot](https://i.stack.imgur.com/TDT0W.png)
var posts = [Post]()
var songs = [Song]()
override func viewDidLoad() {
super.viewDidLoad()
let cellNib = UINib(nibName: "PostTableViewCell", bundle: nil)
tableView.register(cellNib, forCellReuseIdentifier: "postCell")
let songNib = UINib(nibName: "SongTableViewCell", bundle: nil)
tableView.register(songNib, forCellReuseIdentifier: "songCell")
У меня есть 2 разных перья, и я могу извлекать данные, но я не уверен, как структурироватьтест indexPath.row, чтобы строки моего табличного представления могли переключать стиль отображаемых ячеек в зависимости от того, к какой группе данных принадлежит массив.В настоящее время у меня есть тест со статической функцией if, но, очевидно, данные «песни» отображаются только после данных «post»
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count + songs.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row < posts.count {
let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! PostTableViewCell
cell.set(post: posts[indexPath.row])
return cell
} else {
let cell2 = tableView.dequeueReusableCell(withIdentifier: "songCell", for: indexPath) as! SongTableViewCell
cell2.set(song: songs[indexPath.row-posts.count])
return cell2
}
}
--------- EDIT ----------
class Post:Item {
var imageURL: String!
convenience init(id: String, author: UserProfile, text: String, timestamp: Double, imageURL: String) {
self.init(id: id, author: author, text: text, timestamp: timestamp, imageURL: imageURL)
self.imageURL = imageURL
}
}
ИЛИ
class Post:Item {
var imageURL: String!
init(id: String, author: UserProfile, text: String, timestamp: Double, imageURL: String) {
super.init(id: id, author: author, text: text, timestamp: timestamp)
self.imageURL = imageURL
}
}