我需要声明一个变量,如下所示:
var cell
if cond {
cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as? CustomCell1
}
else {
cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as? CustomCell2
}
这里的问题是我收到错误Type annotation missing in pattern
。
我应该声明我的变量是什么类型,或者是否有解决方法?
答案 0 :(得分:4)
假设<div class="media">
<span class="media-left">
<img class="media-object" src="assets/images/cds/cd1.png" alt="CD cover for first CD.">
</span>
<div class="media-body">
<h2 class="media-heading">Name of CD</h2>
<ol>
<li>Song<span>Composer</span></li>
<li>Song<span>Composer</span></li>
<li>Song<span>Composer</span></li>
<li>Song<span>Composer</span></li>
<li>Song<span>Composer</span></li>
<li>Song<span>Composer</span></li>
</ol>
</div>
</div>
<div class="media">
<span class="media-left">
<img class="media-object" src="assets/images/cds/cd2.png" alt="CD cover for second CD.">
</span>
<div class="media-body">
<h2 class="media-heading">Name of CD</h2>
<ol>
<li>Song<span>Composer</span></li>
<li>Song<span>Composer</span></li>
<li>Song<span>Composer</span></li>
<li>Song<span>Composer</span></li>
<li>Song<span>Composer</span></li>
<li>Song<span>Composer</span></li>
<li>Song<span>Composer</span></li>
<li>Song<span>Composer</span></li>
</ol>
</div>
</div>
和CustomCell1
继承自CustomCell2
,您可以执行以下操作:
UICollectionViewCell
然后,当您想将其用作特定类型的单元格时,请使用:
var x: UICollectionViewCell?
if cond {
x = CustomCell1()
}
else {
x = CustomCell2()
}