美文网首页
一个简单的清单 demo

一个简单的清单 demo

作者: _浅墨_ | 来源:发表于2019-01-09 07:40 被阅读19次
    Todoey
    本 demo 特色功能:
    1. Navigation Item Large Title
    2. 多个视图继承自定义的 SwipeTableViewController (使用 SwipeCellKit 封装的可编辑 TableView)
    3. realm 缓存数据到本地
    4. 模糊搜索
    5. 动态 cell 背景色优化
    6. navigationBar 背景色优化
    7. ChameleonFramework (变色龙Framework)使用
    主要代码:
    功能2:

    SwipeTableViewController:

    import UIKit
    import SwipeCellKit
    
    class SwipeTableViewController: UITableViewController, SwipeTableViewCellDelegate {
        
        var cell: UITableViewCell?
    
        override func viewDidLoad() {
            super.viewDidLoad()
            tableView.rowHeight = 80.0
        }
    
        // TableView Datasource Methods
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! SwipeTableViewCell
            cell.delegate = self
            return cell
        }
    
        func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
            
            guard orientation == .right else { return nil }
            let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
                // handle action by updating model with deletion
                self.updateModel(at: indexPath)
            }
            
            // customize the action appearance
            deleteAction.image = UIImage(named: "delete-icon")
            return [deleteAction]
        }
        
        func tableView(_ tableView: UITableView, editActionsOptionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> SwipeTableOptions {
            var options = SwipeTableOptions()
            options.expansionStyle = .destructive
            return options
        }
        
        func updateModel(at indexPath: IndexPath) {
            // Update our data model.
            print("Item deleted from superclass")
        }
    
    }
    
    功能4:
    extension TodoListViewController: UISearchBarDelegate {
        
        func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
            todoItems = todoItems?.filter("title CONTAINS[cd] %@", searchBar.text!).sorted(byKeyPath: "dateCreated", ascending: true)
            tableView.reloadData()
        }
        
        
        func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
            if searchBar.text?.count == 0 {
                loadItems()
                DispatchQueue.main.async {
                    searchBar.resignFirstResponder()
                }
            }
        }
    }
    
    功能6:
    func updateNavBar(withHexCode colourHexCode: String){
            
            guard let navBar = navigationController?.navigationBar else {fatalError("Navigation controller does not exist.")}
            guard let navBarColour = UIColor(hexString: colourHexCode) else { fatalError()}
            navBar.barTintColor = navBarColour
            navBar.tintColor = ContrastColorOf(navBarColour, returnFlat: true)
            navBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor : ContrastColorOf(navBarColour, returnFlat: true)]
            searchBar.barTintColor = navBarColour
            
        }
    
    功能7:
    navBar.tintColor = ContrastColorOf(navBarColour, returnFlat: true)
    

    方法 ContrastColorOf() 作用是根据背景色生成相应的黑色或白色。本例中 navBar 字体颜色根据 navBarColour 背景色的不同而生成或白或黑的颜色,提高视觉体验。比如背景色为黑色的时候,字体显示为白色,背景色为白色的时候,字体显示为黑色。

    ContrastColorOf 方法具体注释:

    Creates and returns either a black or white color object depending on which contrasts more with a specified color.
     
     - parameter backgroundColor: The specified color of the contrast color that is being requested.
     - parameter returnFlat:      Pass **true** to return flat color objects.
     
     - returns: A UIColor object in the HSB colorspace.
    
    demo下载

    参考:

    1. Chameleon
    2. Chameleon——轻量级 iOS 颜色框架

    相关文章

      网友评论

          本文标题:一个简单的清单 demo

          本文链接:https://www.haomeiwen.com/subject/xzbbrqtx.html