CollectionViewCells 不显示任何数据。

huangapple go评论99阅读模式
英文:

CollectionViewCells do not display any data

问题

这是您提供的代码的翻译部分:

  1. 我在项目的另一个区域成功地使用了CollectionViews,并且没有任何问题,但是这个地方无论我怎么做,它都不会在单元格中显示数据。我可以看到单元格的边框,它的宽度和高度是正确的,当我逐步执行代码时,我看到数据被分配到单元格中的标签上。我看到了与源中的字符串列表相对应的正确数量的单元格,但没有显示任何内容。我无法弄清楚为什么。有人看到我的代码有什么问题吗?这应该很简单,但我已经做了3天了,没有结果。
  2. 我的单元格代码:
  3. public partial class PhoneNumberCell : UICollectionViewCell
  4. {
  5. public static readonly NSString Key = new NSString("PhoneNumberCell");
  6. UILabel lblPhoneNumberInfo = new UILabel();
  7. [Export("initWithFrame:")]
  8. public PhoneNumberCell(CGRect frame) : base(frame)
  9. {
  10. // 我设置了框架和文本颜色,因为在其他实例中,数据没有显示出来,这被证明是问题所在。
  11. // 显然在这里不起作用,我删除这段代码后单元格中仍然没有文本。
  12. lblPhoneNumberInfo.Frame = new CGRect(0,0,ContentView.Frame.Width, 26f);
  13. lblPhoneNumberInfo.TextColor = UIColor.Black;
  14. // 但我确实看到了单元格周围的边框。
  15. ContentView.Layer.BorderColor = UIColor.LightGray.CGColor;
  16. ContentView.Layer.BorderWidth = 2.0f;
  17. ContentView.BackgroundColor = UIColor.White;
  18. ContentView.Transform = CGAffineTransform.MakeScale(0.8f, 0.8f);
  19. }
  20. public void SetData(string pPhoneNumberInfo)
  21. {
  22. lblPhoneNumberInfo.Text = pPhoneNumberInfo;
  23. }
  24. protected PhoneNumberCell(IntPtr handle) : base(handle)
  25. {
  26. // 注意:此构造函数不应包含任何初始化逻辑。
  27. }
  28. }

这段代码描述了您的单元格类,但是您的问题可能出现在其他地方。如果需要继续翻译其他部分,请提供详细信息。

英文:

I've used CollectionViews in my project in another area of my project successfully with no issues, but this one simply won't display the data in the cell no matter what I do. I can see the cell border, it is the correct width and height, and when I step through the code I see the data get assigned to the label in the cell. I see the correct number of cells for the list of strings that's in the source, just get nothing displayed. I cannot figure out why. Anyone see any problems with my code? This should be pretty simple, and I've been at it for 3 days now with no results.

My Cell Code:

  1. public partial class PhoneNumberCell : UICollectionViewCell
  2. {
  3. public static readonly NSString Key = new NSString("PhoneNumberCell");
  4. UILabel lblPhoneNumberInfo = new UILabel();
  5. [Export("initWithFrame:")]
  6. public PhoneNumberCell(CGRect frame) : base(frame)
  7. {
  8. // I set the frame and text color because in other instances
  9. // of data not showing up, that's proved to be the issue.
  10. // Apparently not here, I remove this code and still no text in the cell.
  11. lblPhoneNumberInfo.Frame = new CGRect(0,0,ContentView.Frame.Width, 26f);
  12. lblPhoneNumberInfo.TextColor = UIColor.Black;
  13. // I do see the border around the cell though.
  14. ContentView.Layer.BorderColor = UIColor.LightGray.CGColor;
  15. ContentView.Layer.BorderWidth = 2.0f;
  16. ContentView.BackgroundColor = UIColor.White;
  17. ContentView.Transform = CGAffineTransform.MakeScale(0.8f, 0.8f);
  18. }
  19. public void SetData(string pPhoneNumberInfo)
  20. {
  21. lblPhoneNumberInfo.Text = pPhoneNumberInfo;
  22. }
  23. protected PhoneNumberCell(IntPtr handle) : base(handle)
  24. {
  25. // Note: this .ctor should not contain any initialization logic.
  26. }
  27. }

The XIB:

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6211" systemVersion="14A298i" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
  3. <dependencies>
  4. <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7026.1" />
  5. </dependencies>
  6. <objects>
  7. <placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" />
  8. <placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder" />
  9. <collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" customClass="PhoneNumberCell" id="cZE-iV-UFb">
  10. <rect key="frame" x="0.0" y="0.0" width="50" height="50" />
  11. <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES" />
  12. <view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
  13. <rect key="frame" x="0.0" y="0.0" width="50" height="50" />
  14. <autoresizingMask key="autoresizingMask" />
  15. <color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite" />
  16. </view>
  17. </collectionViewCell>
  18. </objects>
  19. </document>

The Source:

  1. public class PhoneNumberSource : UICollectionViewSource
  2. {
  3. public IList<string> PhoneNumbers;
  4. public override nint NumberOfSections(UICollectionView collectionView)
  5. {
  6. return 1;
  7. }
  8. public override nint GetItemsCount(UICollectionView collectionView, nint section)
  9. {
  10. return PhoneNumbers.Count;
  11. }
  12. public PhoneNumberSource(IList<string> pPhoneNumbers)
  13. {
  14. PhoneNumbers = pPhoneNumbers;
  15. }
  16. public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
  17. {
  18. var phoneNumberCell = (PhoneNumberCell)collectionView.DequeueReusableCell(PhoneNumberCell.Key, indexPath);
  19. string phoneinfo = PhoneNumbers[indexPath.Row];
  20. phoneNumberCell.SetData(phoneinfo);
  21. return phoneNumberCell;
  22. }
  23. }

My CollectionView layout:

  1. PhoneNumberList.CollectionViewLayout = new UICollectionViewFlowLayout()
  2. {
  3. ItemSize = new CGSize((float)UIScreen.MainScreen.Bounds.Size.Width, 26f),
  4. HeaderReferenceSize = new CGSize(25, 25),
  5. SectionInset = new UIEdgeInsets(0, 0, 0, 0),
  6. ScrollDirection = UICollectionViewScrollDirection.Vertical,
  7. MinimumInteritemSpacing = 0, // minimum spacing between cells
  8. MinimumLineSpacing = 0,
  9. };

答案1

得分: 0

解决方案是您需要像下面这样为ContentView添加AddSubview

  1. ContentView.AddSubview(lblPhoneNumberInfo);
英文:

The solution is that you need to AddSubview for the ContentView like below:

  1. ContentView.AddSubview(lblPhoneNumberInfo);

huangapple
  • 本文由 发表于 2023年6月16日 01:50:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484295.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定