您的位置:首页 > 产品设计 > UI/UE

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

2016-12-29 12:50 387 查看
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

望大神们解答
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐