Wednesday 21 February 2018

Table View and Collection View in Swift

Table View - Swift
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var tblView: UITableView!
var arr_events:NSMutableArray!

override func viewDidLoad() {
        super.viewDidLoad()
      arr_events = NSMutableArray()
        tblView.estimatedRowHeight = 70.0
        tblView.rowHeight = UITableViewAutomaticDimension
    }

//MARK: TABLEVIEW DELEGATE AND DATASOURCE
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
   
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arr_events.count
    }
   
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: events_CellID) as! CustomEventListCell
        cell.selectionStyle = .none
        let model_Calendar = arr_events[indexPath.row]
        let str_time = "\(model_Calendar.set_time)"
        cell.lbl_Time.text = str_time_val
        return cell
    }
   
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
   
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    }

}

Collection View - Swift
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    @IBOutlet var calendar_View: UICollectionView!
var arr_display_values:NSMutableArray!

override func viewDidLoad() {
        super.viewDidLoad()
      arr_dispaly_values = NSMutableArray()
    }

//MARK: COLLECTIONVIEW DELEGATE AND DATASOURCE
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
   
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return arr_dispaly_values.count
    }
   
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: calendar_CellID, for: indexPath as IndexPath) as! CustomCalendarCell
        for view in cell.view_Month.subviews {
            view.removeFromSuperview()
        }
        let arr_month = arr_dispaly_values.object(at: indexPath.row) as! NSArray
        cell.view_Month = self.create_Month_View(viewMonth: cell.view_Month, array_month: arr_month, font_size: 7.5, month_val: indexPath.row+1, year_val: year_value, isMonthView: false)
        cell.backgroundColor = UIColor.white
        return cell
    }
   
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let screen_width = calendar_View.frame.size.width
        let size_of_box = (screen_width-30) / 3
        if (Int(Window.screenWidth)%320 == 0) {
            return CGSize(width: size_of_box, height: size_of_box);
        }
        return CGSize(width: size_of_box, height: size_of_box+20);
    }
   
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("Selected Cell: \(indexPath.row)")
    }
}

No comments: