Ios 获取UITableViewCell内UICollectionViewController的旋转事件
我有一个Ios 获取UITableViewCell内UICollectionViewController的旋转事件,ios,xamarin,rotation,xamarin.ios,uicollectionview,Ios,Xamarin,Rotation,Xamarin.ios,Uicollectionview,我有一个GetCell方法,它返回一个UITableViewCell,在这个方法中我想添加我的UICollectionViewController,但是我能让它显示任何内容的唯一方法是将UICollectionView从控制器添加到单元格中 在我尝试将ViewWillLayoutSubviews()方法添加到我的UICollectionViewController以检测设备旋转时的布局并调整其大小之前,这样做似乎很好。我的问题是这个ViewWillLayoutSubviews()方法从未被调用
GetCell
方法,它返回一个UITableViewCell
,在这个方法中我想添加我的UICollectionViewController
,但是我能让它显示任何内容的唯一方法是将UICollectionView
从控制器添加到单元格中
在我尝试将ViewWillLayoutSubviews()
方法添加到我的UICollectionViewController
以检测设备旋转时的布局并调整其大小之前,这样做似乎很好。我的问题是这个ViewWillLayoutSubviews()
方法从未被调用
但是,当我将它放在继承自UITableView
的类上时,它确实会被调用。但是我不能在这里执行逻辑,因为我需要更改我的UICollectionViewCells
的宽度和高度
非常感谢您对调整uiCollectionViewCell
大小或改进如何将UICollectionViewController
添加到UITableViewCell
的帮助
我的GetCell()
方法:
public override UITableViewCell GetCell(UITableView tv)
{
UITableViewCell cell = tv.DequeueReusableCell(CellKey);
if (cell == null)
{
cell = new UITableViewCell(UITableViewCellStyle.Default, CellKey);
}
UICollectionView collectionView = tileController.CollectionView;
cell.ContentView.AddSubview(collectionView);
return cell;
}
MyViewWillLayoutSubviews
在MyUICollectionViewController
中(但从未被调用):
您使用
UICollectionViewController
而不仅仅使用UICollectionView
并覆盖UICollectionView
的LayoutSubviews
方法有何原因?如果确实需要使用视图控制器版本,则需要将其正确添加为子视图控制器:并签出“实现自定义容器视图控制器”部分。或者,我还认为您可以通过使用UITableView访问可见单元格,在UITableViewController
中完全实现这一点。IndexPathsForVisibleRows
,然后使用UITableView.GetCell抓取每个单元格,然后让单元格修改其内容。最后,我认为viewwilltransitionosize
是监视视图控制器中的旋转和大小更改事件的一种更常见的方法。是否有任何原因让您使用UICollectionViewController
而不仅仅是使用UICollectionView
并覆盖视图的LayoutSubviews
方法UICollectionView
?如果确实需要使用视图控制器版本,则需要将其正确添加为子视图控制器:并签出“实现自定义容器视图控制器”部分。或者,我还认为您可以通过使用UITableView访问可见单元格,在UITableViewController
中完全实现这一点。IndexPathsForVisibleRows
,然后使用UITableView.GetCell抓取每个单元格,然后让单元格修改其内容。最后,我认为viewwilltransitionosize
是监视视图控制器中的旋转和大小更改事件的更常见的方法。
public override void ViewWillLayoutSubviews()
{
base.ViewWillLayoutSubviews();
nfloat mySectionInset = 20f;
nfloat myMinSpacing = 20f;
var screenWidth = UIScreen.MainScreen.Bounds.Width;
nfloat defaultTileWidth;
UICollectionViewFlowLayout flowLayout = Layout as UICollectionViewFlowLayout;
switch (UIApplication.SharedApplication.StatusBarOrientation)
{
case UIInterfaceOrientation.LandscapeLeft:
case UIInterfaceOrientation.LandscapeRight:
defaultTileWidth = (screenWidth - (mySectionInset * 3) - myMinSpacing) / 3;
flowLayout.ItemSize = new CGSize(defaultTileWidth, defaultTileWidth * 0.65f);
break;
case UIInterfaceOrientation.Portrait:
case UIInterfaceOrientation.PortraitUpsideDown:
defaultTileWidth = (screenWidth - (mySectionInset * 2) - myMinSpacing) / 2;
flowLayout.ItemSize = new CGSize(defaultTileWidth, defaultTileWidth * 0.65f);
break;
}
Layout.InvalidateLayout();
}