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
望高手们解答
解决方案:40分