I have a question that uitableview.i has a list of fruits in uitableview and a filter button in the right side navigation bar. how can we sort the list of fruits when we click the filter button. what should i do in the filterList function. Below is the code.please code.
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()
}
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
}
}
}
Viju source
share