无法使用全局变量访问collectionView单元变量

时间:2018-11-26 07:27:20

标签: swift uicollectionview

在这里,我做了一个collectionView单元变量来访问两个对象。但无法访问单元变量内的对象

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    var cell: UICollectionViewCell!
    if collectionView == collectionView1 {
        cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellAttachment", for: indexPath) as! AttachmentCell
        cell.imgAttachment.image = imageArray1[indexPath.row]
        cell.delegate = self
    }
    else if collectionView == collectionView2 {
       cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellAttachmentView", for: indexPath) as! AttachmentViewCell
       cell.imgFileIcon.image = imgArray2[indexPath.row].fileIcon
    }
    return cell
  

“ UICollectionViewCell”类型的值?没有成员“ imgAttachment”

2 个答案:

答案 0 :(得分:3)

问题出在这里。

var cell: UICollectionViewCell!

您已声明该单元格为UICollectionViewCell类型。因此,无论您将哪个子类存储在其中,您的单元格都只会是UICollectionViewCell类型。

您应该像这样更改它,

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if collectionView === collectionView1 {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellAttachment", for: indexPath) as! AttachmentCell
        cell.imgAttachment.image = imageArray1[indexPath.row]
        cell.delegate = self
        return cell
    } else if collectionView === collectionView2 {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellAttachmentView", for: indexPath) as! AttachmentViewCell
        cell.imgFileIcon.image = imgArray2[indexPath.row].fileIcon
        return cell
    } else {
        // Return the proper cell for other cases
    }
}

或者,如果您坚持要在委托人的末尾只需要一个return语句,那么您可以这样做,

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    var yourCell: UICollectionViewCell!
    if collectionView === collectionView1 {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellAttachment", for: indexPath) as! AttachmentCell
        cell.imgAttachment.image = imageArray1[indexPath.row]
        cell.delegate = self
        yourCell = cell
    } else if collectionView === collectionView2 {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellAttachmentView", for: indexPath) as! AttachmentViewCell
        cell.imgFileIcon.image = imgArray2[indexPath.row].fileIcon
        yourCell = cell
    } else {
        // Return the proper cell for other cases
    }
    return yourCell
}

答案 1 :(得分:2)

您需要通过将services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateIssuerSigningKey = true, ValidIssuer = "mysite.com", ValidAudience = "mysite.com", ClockSkew=TimeSpan.Zero, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("mykey")) }; }); 强制转换为cell来使编译器静默。参见下面的示例,

AttachmentCell

编译器无法识别该变量的原因是将变量(cell as! AttachmentCell).imgAttachment.image = imageArray1[indexPath.row] (cell as! AttachmentCell).delegate = self 声明为cell。由于UICollectionViewCell中没有imgAttachment变量,因此编译器会抱怨。