Good day. in the course of writing the program encountered such a problem. There is an array of json results where various data is stored, including the url of the image that needs to be displayed in the uitableviewcell. the task is to display two images in one cell when the device is rotated to landscape orientation. so far, it turned out to display two identical images, in other cases the index went beyond the array, and the first image from the next cell repeated the second image from the previous one. How is it necessary to edit my code so that you can display two different images from the array of results and not face the fact that the next cell displays the second image from the previous one?

actually, code:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell switch UIDevice.current.orientation { case UIDeviceOrientation.landscapeLeft, UIDeviceOrientation.landscapeRight: //view two pictures in cell when device orientation is landscape cell.entry = self.results?[indexPath.row] cell.nextEntry = self.results?[indexPath.row] default: cell.entry = self.results?[indexPath.row] } return cell } 

necessary data is transferred to the enrty and nextentry cell (entry for the first imageview, nextentry for the second)

  • Ie you need when turning to landscape display the same information in 2 columns? Or something extra? - VAndrJ
  • in fact, yes. In portrait orientation, everything is displayed as in a normal tableview, in one cell one image. when turning, I need to display images in two columns - Darthroid

2 answers 2

A more correct option is to use a UICollectionView, then 1, 2, n columns can be configured without scrambles.

According to your version - as stated in the answer above, do tableView.reloadData()

But at the same time, when in landscape orientation, it is necessary to reduce the number of cells by half, in your example:

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { switch UIDevice.current.orientation { case UIDeviceOrientation.landscapeLeft, UIDeviceOrientation.landscapeRight: return Int(round(Double(cellsCount)/2)) default: return cellsCount } } 

And give the cell:

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell switch UIDevice.current.orientation { case UIDeviceOrientation.landscapeLeft, UIDeviceOrientation.landscapeRight: //view two pictures in cell when device orientation is landscape cell.entry = self.results?[indexPath.row * 2] cell.nextEntry = self.results?[indexPath.row * 2 + 1] default: cell.entry = self.results?[indexPath.row] } return cell } 

Do not forget to check for going beyond the array.

  • thanks, redid your implementation under UICollectionView - Darthroid

1) When you rotate the device, you need to do tableView.reloadData() otherwise your table does not know that you need to reboot and change the image.

2) Since the cells are reused, the picture can be repeated, to avoid this problem, drop the picture in nil ( cell.nextEntry = nil ) and then set the desired picture ( cell.nextEntry = self.results?[indexPath.row] )