// EventStatus Types
enum EventStatusType: String {
case ALL = "ALL"
case LIVE_NOW = "LIVE_NOW"
case HAPPENING_SOON = "HAPPENING_SOON"
case COMING_UP = "COMING_UP"
case ENDING_NOW = "ENDING_NOW"
}
class FilterTableHandler: NSObject, UITableViewDelegate,UITableViewDataSource {
public typealias FilterTableCallback = ( Int, String ) -> Void
private var callback: FilterTableCallback?
var filterListArray = [FilterCellObject]()
var table : UITableView!
override init() {
super.init()
}
func initializeFilterList() {
filterListArray.append(FilterCellObject.init(title: "Live Now" , imageName: "filterGreen", querystring: EventStatusType.LIVE_NOW.rawValue, selectionColor: UIColor(red: 0.973, green: 0.996, blue: 0.914, alpha: 1.00)))
filterListArray.append(FilterCellObject.init(title: "Happening Soon" , imageName: "filterYellow", querystring: EventStatusType.HAPPENING_SOON.rawValue, selectionColor: UIColor(red: 0.976, green: 0.925, blue: 0.902, alpha: 1.00)))
filterListArray.append(FilterCellObject.init(title: "Coming Up" , imageName: "filterOrange", querystring: EventStatusType.COMING_UP.rawValue, selectionColor: UIColor(red: 1.000, green: 0.945, blue: 0.918, alpha: 1.00)))
filterListArray.append(FilterCellObject.init(title: "All" , imageName: "", querystring: EventStatusType.ALL.rawValue, selectionColor: UIColor(red: 1.000, green: 1.000, blue: 1.000, alpha: 1.00)))
}
init(tableView: UITableView,callback: @escaping FilterTableCallback) {
super.init()
initializeFilterList()
self.callback = callback
table = tableView
tableView.allowsSelection = true
tableView.separatorStyle = .none
tableView.backgroundColor = UIColor.clear
tableView.bounces = false
tableView.register(UINib(nibName: "FilterTableViewCell", bundle: nil), forCellReuseIdentifier: "FilterTableViewCell")
tableView.delegate = self
tableView.dataSource = self
table.delegate = self
table.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.filterListArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FilterTableViewCell", for: indexPath) as! FilterTableViewCell
cell.lblTitle.text = self.filterListArray[indexPath.row].title
if self.filterListArray[indexPath.row].imageName.isNotEmpty {
cell.colorImgView.image = UIImage(named:self.filterListArray[indexPath.row].imageName)
}
cell.customBackgroundView.backgroundColor = self.filterListArray[indexPath.row].backGroundSelectionColor
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: false)
self.callback?(indexPath.row,self.filterListArray[indexPath.row].querystring)
}
}
This class handles all table view delegate and dataSource.
And returns data.
Just design TableView and create IBOutlet of that table without adding cell in the storyboard. And create your tableview cell using xib to make it reusable.
Then, in your controller use FilterTableHandler like this
var filerHandler = FilterTableHandler.init()
self.filerHandler = FilterTableHandler.init(tableView: self.filterTableView, callback: { (index,queryString) in
//Your actions on cell selection
self.hideFilterView()
self.hideInfoView()
self.getEventList(queryString: queryString)
if index != 3 {
//filter option selected
self.btnFilter?.setImage(UIImage(named:"filterFilled"), for: .normal)
}
else {
//"all" option selected
self.btnFilter?.setImage(UIImage(named:"filter"), for: .normal)
}
})
Initialize FilterTableHandler object as shown, And receive selected data using custom callbacks.
You can customize it as per your requirement.
And tableview becomes reusable for whole app.