UICollectionView使用reloadData()方法之后,Cell 里面的控件报错找不到

头发还没秃a 头发还没秃a 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
NSLog("\(indexPath.row)")
let collectionCell = collectionView.dequeueReusableCellWithReuseIdentifier("collectionCellLocation", forIndexPath: indexPath) as UICollectionViewCell

let cityLocationButton = collectionCell.viewWithTag(1) as! UIButton
let autoLocationButton = collectionCell.viewWithTag(2) as! UIButton
if indexPath.row > 0 {
NSLog("1111111")
autoLocationButton.hidden = true
cityLocationButton.setTitle(searchCityList[indexPath.row-1], forState: UIControlState.Normal)
}else{
cityLocationButton.hidden = true
}

//添加手动定位点击事件
cityLocationButton.tag = indexPath.row
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "clickForCityName:")
cityLocationButton.addGestureRecognizer(tapGestureRecognizer)
//添加自动定位点击事件
autoLocationButton.tag = indexPath.row
autoLocationButton.addTarget(self, action: "clickAutoLocation:", forControlEvents: UIControlEvents.TouchUpInside)

return collectionCell
}


刚进来的时候可以正常显示控件,但是reloadData()之后,就报错:

let cityLocationButton = collectionCell.viewWithTag(1) as! UIButton

这一行显示错误信息是:fatal error: unexpectedly found nil while unwrapping an Optional value
望大神们解答
...全文
评论 5
收藏 已收藏
分享
返回顶部
头发还没秃a
iOS 29,037 成员 12,459 内容