我检查了类似的主题,但没有人帮助过我。基本上,我编写了一个自定义UICollectionViewCell
,并且在collectionView中,当我点击其中任何一个时,didSelectItemAtIndexPath
方法都不会被触发。
我重写了我的代码以使用标准UICollectionViewCell
,然后就可以了。任何人都可以帮我处理我应该覆盖/更改的内容吗?
自定义单元格:
class UICollectionViewCellWithButton: UICollectionViewCell {
var button = UIButton();
override init(frame: CGRect) {
super.init(frame: frame);
button = UIButton.buttonWithType(UIButtonType.System) as UIButton;
button.setTitle("", forState: UIControlState.Normal);
button.frame = CGRectMake(0, 0, 50, 50);
self.addSubview(button);
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder);
}
/* override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView?{
return self;
}*/
}
收藏视图:
class StartNodeViewController: UICollectionViewController, UICollectionViewDelegate {
var numberOfNodes = Int();
private let reuseIdentifier = "Cell";
var cellColor = true;
var counter = 1;
override func viewDidLoad() {
super.viewDidLoad();
self.navigationItem.title="Select start node";
self.collectionView.registerClass(UICollectionViewCellWithButton.self, forCellWithReuseIdentifier: "Cell");
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
// 1
return 1;
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// 2
return numberOfNodes;
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCellWithButton {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCellWithButton;
cell.button.setTitle(counter.description, forState: UIControlState.Normal);
cell.backgroundColor = UIColor.whiteColor();
counter++;
return cell;
}
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
//super.collectionView(collectionView, didSelectItemAtIndexPath: indexPath);
(collectionView.cellForItemAtIndexPath(indexPath) as UICollectionViewCellWithButton).button.setTitle("0", forState: UIControlState.Normal);
}
}
此时代码可能有点乱,因为我尝试了我发现的任何东西。
答案 0 :(得分:0)
试试这个:
UICollectionViewCellWithButton
。cellForItemAtIndexPath
的返回值更改为UICollectionViewCell
。运行它,它应该是有效的。我希望这能帮到你。
答案 1 :(得分:0)
@rdelmar回答我。我最后切换了标签按钮。