0
Redditのようなユーザーの投稿を並べ替えるアプリケーションを作成しようとしています。カスタムUITableViewCellsの並べ替え(Swift 2、Xcode 7)
ポストにある「投票」(Int値)の数でセルを並べ替える方法を見つけようとしています。
投稿が表示されているビューコントローラで、私はこれのほとんどが関連していると思います。私はまた、ここに演奏されているビューの検索バーを持っています。
以下のコードを実行すると、View Controllerに何も表示されません。
すでに問題は、細胞を選別した関数が呼び出されなかったことに気づいたとして任意の助けもそう
import UIKit
import Firebase
class FeedVC: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
var ref: FIRDatabaseReference!
var posts = [Post]()
static var imageCache = NSCache()
var inSearchMode = false
var filteredVenues = [Post]()
var sortByVotes = [Post]()
private var _post: Post?
var post: Post? {
return _post
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
searchBar.delegate = self
searchBar.returnKeyType = UIReturnKeyType.Done
tableView.estimatedRowHeight = 368
ref = FIRDatabase.database().reference()
DataService.ds.REF_POSTS.observeEventType(.Value, withBlock: { snapshot in
print(snapshot.value)
self.sortByVotes = []
if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshots {
print("SNAP: \(snap)")
if let postDict = snap.value as? Dictionary<String, AnyObject> {
let key = snap.key
let post = Post(postKey: key, dictionary: postDict)
self.sortByVotes.append(post)
}
}
}
self.tableView.reloadData()
})
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if inSearchMode {
return filteredVenues.count
}
return sortByVotes.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//let post = posts[indexPath.row]
if let cell = tableView.dequeueReusableCellWithIdentifier("PostCell") as? PostCell {
cell.request?.cancel()
var img: UIImage?
let post: Post!
posts = posts.sort{$0.votes > ($1.votes)} // added this*****
if inSearchMode {
post = filteredVenues[indexPath.row]
} else {
post = sortByVotes[indexPath.row]
}
if let url = post.imageUrl {
img = FeedVC.imageCache.objectForKey(url) as? UIImage
}
cell.configureCell(post, img: img)
return cell
} else {
return PostCell()
}
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let post = sortByVotes[indexPath.row]
if post.imageUrl == nil {
return 200
} else {
return tableView.estimatedRowHeight
}
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
view.endEditing(true)
}
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
if searchBar.text == nil || searchBar.text == "" {
inSearchMode = false
view.endEditing(true)
posts = posts.sort{$0.votes > ($1.votes)} //added this ***
tableView.reloadData()
} else {
inSearchMode = true
let lower = searchBar.text!.lowercaseString
filteredVenues = sortByVotes.filter({$0.venueName!.rangeOfString(lower) != nil})
tableView.reloadData()
}
}
func sortPosts(){ //removed this function******
sortByVotes = posts.sort { $0.votes > ($1.votes)}
tableView.reloadData()
}
}
あなたは ')(' FUNCのsortPostsを呼んでいますどこに? –
Hey Victor、私はその機能を取り除き、 'textDidChange'関数と' cellForRowAtIndexPath'関数で 'posts = posts.sort {$ 0.votes>($ 1.votes)}'を追加しました、上記を参照 – ryanbilak
しかし今は動作しています? –