I have long admired the fitting of bloggers. Later, I would like to read the code constraint for navigation, so I would like to bring the constraint to Swift. The team of navigation has also developed a SnapKit library for the constraint of Swift. Cell adaptation was previously written with Object-C, but it used a relative layout and did not support landscape. The code here uses Swift on the basis of the original, with some improved details, which are slowly introduced below:

1. Obtain the image size originally obtained during cell layout, but in order to obtain the image size is to initiate a network request, so, in addition to blocking the main thread will also make the TableView scroll incredibly. The solution is to directly obtain the size of the image in the download thread when the data is downloaded and store it in the dictionary for use below, because the size is available, regardless of the horizontal and vertical screen as long as there is a proportion of the size, it can be perfectly adapted.

2. Use native methods to request back to the main thread to refresh the UI front by bloggers have sent the request of the how to use native methods to access the network, but in this Demo establishment of principal a low-level mistakes, because the original request is in itself a thread, and bloggers with third party use habits, directly at the completion of a request to refresh the tableview, This child thread will block the main thread, so you will see that you wait a long time to refresh the data. The correct way to do this is to return to the main thread:

 dispatch_async(dispatch_get_main_queue(), {
                print("OK")
                self.creatTableView()
                self._tableView.reloadData()

            })
Copy the code

3. Screen width and height changes in horizontal and vertical screen

var WIDTH = UIScreen.mainScreen().bounds.size.width

var HEIGHT = UIScreen.mainScreen().bounds.size.height
Copy the code

There are several ways to get the screen width and height. This is just one of them. Does it change in landscape? As a matter of fact, there is no change in the height and width, so we need to check the current screen state:

if UIDevice.currentDevice().orientation == .Portrait  
{
                width = WIDTH
}
else
{
                width = HEIGHT
}
Copy the code

So the horizontal and vertical screens fit. Here do not put the code, the need to download: github.com/codeliu6572…