phuc-n
10/3/2017 - 2:32 AM

TableView

Test TabelView

    func setCellWithTodoItem(_ cell: UITableViewCell, todo: ToDoItem){
        let imageView: UIImageView = cell.viewWithTag(11) as! UIImageView
        let titleLabel: UILabel = cell.viewWithTag(12) as! UILabel
        let dateLabel: UILabel = cell.viewWithTag(13) as! UILabel
        
        imageView.image = UIImage(named: todo.image)
        titleLabel.text = todo.title
        dateLabel.text = stringFromDate(todo.date)
    }
    
    func setMessageLabel(_ messageLabel: UILabel,frame: CGRect,text: String, textColor: UIColor, numberOfLines: Int, textAlignment: NSTextAlignment, font: UIFont ){
        messageLabel.frame = frame
        messageLabel.text = text
        messageLabel.textColor = textColor
        messageLabel.numberOfLines = numberOfLines
        messageLabel.textAlignment = textAlignment
        messageLabel.font = font
        messageLabel.sizeToFit()
    }
extension ViewController: UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if todos.count != 0 {
            self.todoTableView.separatorStyle = .singleLineEtched;
            self.todoTableView.backgroundView = nil;
            return todos.count
        }else{
            let messageLabel: UILabel = UILabel()
            
            setMessageLabel(messageLabel, frame: CGRect(x: 0,y:0 ,width: self.view.bounds.size.width, height: self.view.bounds.size.height), text: "No Data is currently available", textColor: UIColor.black, numberOfLines: 0, textAlignment: .center, font: UIFont(name:"Palatino-Italic",size: 20)!)
            
            self.todoTableView.backgroundView = messageLabel
            self.todoTableView.separatorStyle = .none
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier: String = "todoCell"
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
        
        setCellWithTodoItem(cell, todo: todos[indexPath.row])
        return cell
    }
}
extension ViewController: UITableViewDelegate{
    //Edit mode
    override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
        todoTableView.setEditing(editing, animated: true)
    }
    
    // Delete the cell
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == UITableViewCellEditingStyle.delete{
            todos.remove(at: indexPath.row)
            todoTableView.deleteRows(at: [indexPath], with: .automatic)
        }
    }
    
    // Move the cell
    func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        return self.isEditing
    }
    
    func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        let todo = todos.remove(at: sourceIndexPath.row)
        todos.insert(todo, at: destinationIndexPath.row)
    }
}