0

Can we use a different TableViewCell for tableview when the device is in portrait mode and different TableViewCell when device turns to landscape mode?

user229044
  • 232,980
  • 40
  • 330
  • 338
Arsh Bhullar
  • 61
  • 1
  • 5
  • 1
    You can use `UICollectionView` instead, and handle vertical and horizontal in different modes – Mac3n Feb 27 '20 at 06:48

2 Answers2

3

Yes sure.

You can use the below lines of code to check for the current orientation and then decide which cell to use in your cellForRowAtIndexPath method.

if UIDevice.current.orientation == .portrait {
    //first cell
} else {
    //second cell
}

I think you would also need a trigger point for your table view to reload it's cells.

You can override the below method and then call the reloadData method whenever needed.

override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
}
schinj
  • 794
  • 4
  • 19
  • it displays the landscape mode cell when the app launches and I have to rotate the device to landscape and portrait to get the portrait cell dequeued. – Arsh Bhullar Feb 27 '20 at 08:55
  • var cell = UITableViewCell() if UIDevice.current.orientation == .portrait { cell = tableView.dequeueReusableCell(withIdentifier: "CustomerTableViewCell", for: indexPath) //cell.textLabel?.text = "hey how are yoy baby" } else { cell = tableView.dequeueReusableCell(withIdentifier: "SecondCell", for: indexPath) cell.textLabel?.text = "hbgusgdbdshfgdfjhdgjhmdngjsh" } return cell } and then reloaded the cell in viewWillTransition method. – Arsh Bhullar Feb 28 '20 at 04:42
  • @ArshBhullar Regarding getting the correct orientation there are other answers too. Please check them once. https://stackoverflow.com/questions/34452650/in-swift-how-to-get-the-device-orientation-correctly-right-after-its-launched – schinj Mar 03 '20 at 05:45
0

like above, butfunc didRotate is deprecated, instead use

viewWillTransition(to size: CGSize, 
               with coordinator: UIViewControllerTransitionCoordinator)
Bartosz Jarosz
  • 188
  • 1
  • 5