Ios download and display images in tableview






















Open now the main. Under the file inspector we can disable the use size classes feature , we don't need that. Add inside the UIViewController the CollectionView , set the size to fit the screen size, and add the following constraints for the top, left, right and bottom.

Select now the cell of the UICollectionView , under the size inspector change size to Custom and add a width and height of This parameter is not used by swift when is rendering the cell on the iOS App, but allows us just to define better the custom cell. Align it and resize it inside the cell as you want and at the constraints. Here are the contraints I've used:. In order to have a custom cell , we need also a custom class. Let's create it. And for class choose the name you want, we will use in this case CellClass.

Turn now back to the main. Under the identity inspector write the class name you've previously created CellClass in my case and under the attribute inspector write " cell " as an identifier or write the name you want. As a variable name I wrote collectionview , but be free to put the name you want. It's now time to download the images and to display them inside the cells.

Open the ViewController. The images cache array allows to keep for the current session for all the time the app is opened the downloaded images in a cache , this allows to avoid to download them again while scrolling. In fact every time the user scrolls the CollectionView , the cells are reused. Reusing a cell is something like destroying the previous content and display the new one.

So we'll keep for this reason the content stored. The image array allows us to keep the json data structure that defines the links of the images. The link String defines the link of the json of the images. In this example I've prepared for you a simple array of images.

You can find it here. For getting the links of the images, we'll use the json format. Let's define the functions for downloading and parsing the data.

In this ImageView the image will be displayed as soon as is arrived. Finally let's initialize the cell's width and height , the UICollectionView data source class and delegate. The numberOfItemsInSection method uses the images array to let Swift know how many cells are to display. Remeber to put as a dequeueReusableCellWithReuseIdentifier the reusable identifier that you wrote under the attribute inspector. Here you can download the final project.

Here you can download the project for Swift 3. Once we open Xcode the welcome window will open like as shown below. The new Xcode window will contain several built-in app templates to implement the common type of iOS apps like page-based apps, tab-based apps, games, table-view apps, etc. These templates are having a pre-configured interface and source code files. After click Next we will get a window like as shown below.

Enter your organization identifier in case if you don't have any organization identifier enter com.

Bundle Identifier : This value will generate automatically based on the values we entered in the Product Name and Organization Identifier.

Choose Devices options as Universal it means that one application is for all Apple devices in case if you have any specific requirement to run an app only for iPad then you can choose the iPad option to make your application restricted to run only on iPad devices.

This option is used for database operations. In case if you have any database related operations in your application select this option otherwise unselect the option. In case if you need unit tests for your application then select this option otherwise unselect it.

Once we click on the Next button new dialog will open in that we need to select the location to save our project. Once you select the location to save the project then click on Create button like as shown below. After clicking on the Create button, the Xcode will create and open a new project. In our project, Main. Now in project select Main. Now select ViewController. Learn more. Asked 4 years, 6 months ago. Active 4 years, 6 months ago. Viewed times. Improve this question.

Don't use tags. Why not simply supply the array of images to your cell and handle the buttons within the cell class? Also, you are adding your button handler every time when cells are reused. Paulw11 didnt get you,can u be more speciific — tnishanth. You are trying to handle the button taps in your table view controller, which means that you need to identify which cell the button tap happened in, for which you are using the tag. Tags are fragile since cells are reused and can be reordered.

If you simply set the array of images as a property on your custom cell class then each cell can handle its own button taps and just move through its array of images. Add a comment. Active Oldest Votes. Improve this answer. Paulw11 Paulw11



0コメント

  • 1000 / 1000