我的问题:
我想要数组的计数来确定有多少UICollectionVIEwCell.
每个UICollectionVIEwCell都包含一个按钮.单击后,我希望此按钮使数组中与UICollectionVIEwCell编号对应的数据显示在标签中.
例如,如果用户单击第13个UICollectionVIEwCell的按钮,则数组中的第13个Nsstring将成为UILabel的文本.
我做了什么:
我已经为我用于所有UICollectionVIEwCells的nib文件创建了自己的UICollectionVIEwCell子类,&将按钮连接到.h文件作为IBAction.我还导入了MainVIEwController.h,它包含存储Nsstrings的数组属性.
当我在UICollectionVIEwCell的动作中编辑代码时,我无法访问数组属性.按钮确实有效 – 我在IBAction的方法中放置了一个NSLog,它确实有效.
我在SO上搜索了数十个其他答案,但没有人回答我的具体问题.如果需要,我可以使用我的代码示例更新此内容.
解决方法I have made my own subclass of UICollectionVIEwCell for the nib file
that I use for all of the UICollectionVIEwCells,and connected the
button to the .h file as a IBAction.
如果将IBAction连接到collectionVIEwCell的子类,则需要创建一个委托,以便在显示数据的vIEwController中使触摸事件可用.
一个简单的调整是添加collectionVIEwCell按钮,将它的IBOutlet连接到单元格.但不是IBAction.在cellForRowAtIndexPath中:在包含collectionVIEw的vIEwController中为按钮添加一个eventHandler.
- (UICollectionVIEwCell *)collectionVIEw:(UICollectionVIEw *)collectionVIEw cellForItemAtIndexPath:(NSIndexPath *)indexPath{ //Dequeue your cell [cell.button addTarget:self action:@selector(collectionVIEwCellbuttonpressed:) forControlEvents:UIControlEventtouchUpInsIDe]; return cell;}- (IBAction)collectionVIEwCellbuttonpressed:(UIbutton *)button{ //Acccess the cell UICollectionVIEwCell *cell = button.superVIEw.superVIEw; NSIndexPath *indexPath = [self.collectionVIEw indexPathForCell:cell]; Nsstring *Title = self.strings[indexPath.row]; self.someLabel.text = Title;}总结
以上是内存溢出为你收集整理的ios – 使用UICollectionViewCell上的按钮显示数组中的数据全部内容,希望文章能够帮你解决ios – 使用UICollectionViewCell上的按钮显示数组中的数据所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)