Swift Coding Summary 3

1. Code conversion:

dataString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
requestData.stringValue.removingPercentEncoding ?? ""

2. The value needs to be judged to be empty and set to be empty to prevent the value from crashing:

                // Total number of pages 
                let totalPageCount = dicData?[ " recordNum " ] as ? String
                 // Dish data source 
                var dataSource = dicData?[ " orderInfo " ] as ? [Dictionary<String,Any>] ?? [Dictionary<String, Any>]()

3. You can add refresh in UIView, and pass the block to the controller to operate the network request:

        //上拉加载
        historyTableView.mj_footer = MJRefreshAutoNormalFooter(refreshingBlock : {[weak self]() -> Void in
            self?.refreshBack?()
        })

4. The difference between setNeedsLayout and layoutIfNeeded:

This method layoutSubviews cannot be called directly. Forcibly refresh the layout, call setNeedsLayout, if you want to refresh the interface immediately, call layoutIfNeeded

setNeedsLayout

Marked as needing to be re-layout, not refreshed immediately, but layoutSubviews will definitely be called

Update immediately with layoutIfNeeded

layoutIfNeeded

If there is a marker that needs to be refreshed, call layoutSubviews immediately for layout

Generally written in UIView animation.

https://yiweifen.com/html/news/WaiYu/74770.html

http://ios.jobbole.com/84219/

5.For loop is recommended to use:

 for (index,item) in potTaste.details.enumerated() {  }

 data.forEach { (label, model) in {  }

6. Enumerate different types and set different properties:

enum ButtonName {
     case Split(String,UIColor,UIColor)                   // split 
    case Doubly(String,UIColor,UIColor)                  // multiply 
    case Delay(String,UIColor,UIColor)                   // delay 
    case SureOrder(String,UIColor,UIColor)               / / Confirm the order 
    case Rush(String,UIColor,UIColor)                    // The food reminder 
    case Confirm(String,UIColor,UIColor)                 // Confirm the ordering 
    case Gift(String,UIColor,UIColor)                    // Gift food 
    caseGiveBack(String,UIColor,UIColor)                // Return menu 
    case ChangeTableDish                                 // Single product turntable 
}

7. Tuples can be used to accept multiple models in Cell:

typealias ModelTuple = (dishLabel: DishLabel, dishViewModel: DishViewModel)
 var model: ModelTuple!

8. Optimization of the serious problem of dropped frames when a large number of Items in CollectionView slide quickly:

if #available(iOS 10.0, tvOS 10.0, *) {
            collectionView?.prefetchDataSource = self
        }

9.convert。。。to。。。:

            // Convert the center point of the collectionView in the controller view to the coordinates on the collectionView 
            var topPoint = CGPoint(x: UIScreen.main.bounds.size.width* 0.5 , y: NavigationBarH)
             var pInView = view.convert(topPoint, to : collectionView)

10.MJRefresh automatically switches the transparency according to the drag ratio:

let header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: #selector(loadNew))
 // Set automatic switching transparency (automatically hide under the navigation bar) 
header?.isAutomaticallyChangeAlpha = true ;

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324951559&siteId=291194637