i have a question about uitableview.i have a fruits list in uitableview and a filter button in right side navigation bar. how we can sort a fruits list when we click filter button. what should i do in filterList function. below is code.please look it.
/* Fruit.swift */
import Foundation
class Fruit {
var fruitName = ""
init(fruitName: String) { self.fruitName = fruitName }
}
class FruitsListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var fruitList: UITableView!
@IBOutlet var filterItem: UIBarButtonItem!
var fruitArr:[Fruit] = Fruit
override func viewDidLoad() {
super.viewDidLoad()
var barButton = UIBarButtonItem(title: "Filter", style: .Plain, target: self, action:"filterList")
self.navigationItem.rightBarButtonItem = barButton
var fruits1 = Fruit(fruitName: "Apple" as String)
var fruits2 = Fruit(fruitName: "Mango" as String)
var fruits3 = Fruit(fruitName: "Banana" as String)
var fruits4 = Fruit(fruitName: "Orange" as String)
fruitArr.append(fruits1)
fruitArr.append(fruits2)
fruitArr.append(fruits3)
fruitArr.append(fruits4)
}
func filterList() {/* ? */}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return fruitArr.count
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell:ShowFruitCustomCellTableViewCell = tableView.dequeueReusableCellWithIdentifier('FruitCell') as ShowFruitCustomCellTableViewCell
let fruit = fruitArr[indexPath.row] cell.setCell(fruit.fruitName) return cell
}
}
}