CollectionViewCells 不显示任何数据。

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

CollectionViewCells do not display any data

问题

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

我在项目的另一个区域成功地使用了CollectionViews,并且没有任何问题,但是这个地方无论我怎么做,它都不会在单元格中显示数据。我可以看到单元格的边框,它的宽度和高度是正确的,当我逐步执行代码时,我看到数据被分配到单元格中的标签上。我看到了与源中的字符串列表相对应的正确数量的单元格,但没有显示任何内容。我无法弄清楚为什么。有人看到我的代码有什么问题吗?这应该很简单,但我已经做了3天了,没有结果。

我的单元格代码:

public partial class PhoneNumberCell : UICollectionViewCell
{
    public static readonly NSString Key = new NSString("PhoneNumberCell");

    UILabel lblPhoneNumberInfo = new UILabel();

    [Export("initWithFrame:")]
    public PhoneNumberCell(CGRect frame) : base(frame)
    {
        // 我设置了框架和文本颜色,因为在其他实例中,数据没有显示出来,这被证明是问题所在。
        // 显然在这里不起作用,我删除这段代码后单元格中仍然没有文本。
        lblPhoneNumberInfo.Frame = new CGRect(0,0,ContentView.Frame.Width, 26f);
        lblPhoneNumberInfo.TextColor = UIColor.Black;
        // 但我确实看到了单元格周围的边框。
        ContentView.Layer.BorderColor = UIColor.LightGray.CGColor;
        ContentView.Layer.BorderWidth = 2.0f;
        ContentView.BackgroundColor = UIColor.White;
        ContentView.Transform = CGAffineTransform.MakeScale(0.8f, 0.8f);
    }

    public void SetData(string pPhoneNumberInfo)
    {
        lblPhoneNumberInfo.Text = pPhoneNumberInfo;
    }

    protected PhoneNumberCell(IntPtr handle) : base(handle)
    {
        // 注意:此构造函数不应包含任何初始化逻辑。
    }
}

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

英文:

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:

public partial class PhoneNumberCell : UICollectionViewCell
{
    public static readonly NSString Key = new NSString("PhoneNumberCell");

    UILabel lblPhoneNumberInfo = new UILabel();

    [Export("initWithFrame:")]
    public PhoneNumberCell(CGRect frame) : base(frame)
    {
        // I set the frame and text color because in other instances 
        // of data not showing up, that's proved to be the issue.
        // Apparently not here, I remove this code and still no text in the cell.
        lblPhoneNumberInfo.Frame = new CGRect(0,0,ContentView.Frame.Width, 26f);
        lblPhoneNumberInfo.TextColor = UIColor.Black;
        // I do see the border around the cell though.
        ContentView.Layer.BorderColor = UIColor.LightGray.CGColor;
        ContentView.Layer.BorderWidth = 2.0f;
        ContentView.BackgroundColor = UIColor.White;
        ContentView.Transform = CGAffineTransform.MakeScale(0.8f, 0.8f);
    }

    public void SetData(string pPhoneNumberInfo)
    {
        lblPhoneNumberInfo.Text = pPhoneNumberInfo;
    }

    protected PhoneNumberCell(IntPtr handle) : base(handle)
    {
        // Note: this .ctor should not contain any initialization logic.
    }
}

The XIB:

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

The Source:

public class PhoneNumberSource : UICollectionViewSource
{
    public IList<string> PhoneNumbers;
    public override nint NumberOfSections(UICollectionView collectionView)
    {
        return 1;
    }
    public override nint GetItemsCount(UICollectionView collectionView, nint section)
    {
        return PhoneNumbers.Count;
    }

    public PhoneNumberSource(IList<string> pPhoneNumbers)
    {
        PhoneNumbers = pPhoneNumbers;
    }
    public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
    {
        var phoneNumberCell = (PhoneNumberCell)collectionView.DequeueReusableCell(PhoneNumberCell.Key, indexPath);
        string phoneinfo = PhoneNumbers[indexPath.Row];
        phoneNumberCell.SetData(phoneinfo);
        return phoneNumberCell;
    }
}

My CollectionView layout:

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

答案1

得分: 0

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

ContentView.AddSubview(lblPhoneNumberInfo);
英文:

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

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:

确定