【问题标题】:Why is my UICollectionViewCell not getting data from UICollectionViewController?为什么我的 UICollectionViewCell 没有从 UICollectionViewController 获取数据?
【发布时间】:2020-01-25 02:45:32
【问题描述】:

我有一个名为SwipingControllerUICollectionViewController,它创建了一个TeamCell 并给它一个“波士顿凯尔特人队”的teamName

class SwipingController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView?.backgroundColor = .white
        collectionView?.register(TeamCell.self, forCellWithReuseIdentifier: "cellId")

        collectionView?.isPagingEnabled = true
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 2
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! TeamCell

        cell.teamName = "Boston Celtics"

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: view.frame.width, height: view.frame.height)
    }
}

但是,当我运行代码时,teamName 仍然打印为空字符串。

class TeamCell: UICollectionViewCell {

    var teamName: String =  ""

    override init(frame: CGRect) {
        super.init(frame: frame)

        print(teamName) //this prints nothing
    }
}

【问题讨论】:

  • 你现在黑屏了?
  • @YogeshPatel 不,我的 CollectionViewCells 出现在屏幕上。问题是,TeamCell 中的teamName 是一个空字符串,即使我在SwipingController() 中将其设置为“波士顿凯尔特人队”
  • 请为您的标签分配约束,然后它会显示给您。

标签: ios swift uicollectionview uicollectionviewcell


【解决方案1】:

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! TeamCell

它初始化单元格,因此最初它是空的。

初始化后调用cell.teamName = "Boston Celtics",实际上没有任何作用

你必须更新名字

class TeamCell: UICollectionViewCell {

    var teamName: String =  ""

    override init(frame: CGRect) {
        super.init(frame: frame)

        print(teamName) //this prints nothing
    }

    func updateName(name : String) {
         self.testName = name

    }

}

在集合视图中

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! TeamCell

        cell.updateName(name : "Boston Celtics") 

        return cell
 }

【讨论】:

    【解决方案2】:

    您只是在此过程中过早地查看teamName。当cellForItemAt 到达cell.teamName = ... 行时,单元格的init 方法已经被调用。因此,teamNameinit 方法期间将始终为空白。但是,teamName 将在单元格出现在视图中之前设置。


    通常单元格只有 UIKit 控件,例如 UILabel,而您可以设置其中的 text,例如

    class TeamCell: UICollectionViewCell {
        var teamLabel: UILabel = {
            let label = UILabel()
            label.translatesAutoresizingMaskIntoConstraints = false
            return label
        }()
    
        override init(frame: CGRect) {
            super.init(frame: frame)
    
            configure()
        }
    
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        func configure() {
            addSubview(teamLabel)
    
            NSLayoutConstraint.activate([
                teamLabel.centerXAnchor.constraint(equalTo: centerXAnchor),
                teamLabel.centerYAnchor.constraint(equalTo: centerYAnchor)
            ])
        }
    
        func update(for team: String) {
            teamLabel.text = team
        }
    }
    

    然后

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! TeamCell
    
        cell.update(for: "Boston Celtics")
    
        return cell
    }
    

    【讨论】:

      【解决方案3】:

      请试试这个:

      class TeamCell: UICollectionViewCell {
      
          var teamName : String = "" {
             didSet {
                 print(teamName)
             }
          }
      
          override func awakeFromNib() {
             super.awakeFromNib()
             //custom logic goes here
      
          }
      }
      
      
      override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! TeamCell
      
          cell.teamName = "Boston Celtics"
      
          return cell
      }
      

      class TeamCell: UICollectionViewCell {
      
      
          override func awakeFromNib() {
             super.awakeFromNib()
             //custom logic goes here
      
          }
      
          func getTeamName(teamName : String)
          {
              print(teamName)
          }
      }
      
      
      override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! TeamCell
      
          cell.getTeamName(teamName: "Boston Celtics")
      
          return cell
      }
      

      【讨论】:

        【解决方案4】:

        在 TeamCell 类中试试这个

        var teamcell = String()
        
        override init(frame: CGRect) {
            super.init(frame: frame)
        
            if teamcell != nil {
                print(teamName)
            }
        }
        

        【讨论】:

          【解决方案5】:

          这只是事件顺序的问题。创建单元格,打印,在表格视图中使用单元格,分配团队名称。

          【讨论】:

            猜你喜欢
            • 2019-04-11
            • 1970-01-01
            • 2022-11-23
            • 2011-11-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多