xcode 使用 UICollectionViewCell 上的按钮显示数组中的数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16453205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use a button on a UICollectionViewCell to display data from an array
提问by tagabek
I have an array of NSStrings
, one UILabel
& a UICollectionView
.
我有一个NSStrings
, one UILabel
& a的数组UICollectionView
。
My Question:
我的问题:
I want the array's count to determine how many UICollectionViewCell
's there are.
我想要数组的计数来确定有多少个UICollectionViewCell
。
Each UICollectionViewCell
contains a button. Upon click, I want this button to cause the data in the array that corresponds to the UICollectionViewCell
's number to be displayed in the label.
每个都UICollectionViewCell
包含一个按钮。单击后,我希望此按钮使与UICollectionViewCell
的编号对应的数组中的数据显示在标签中。
For example, if the user clicks on the 13th UICollectionViewCell
's button, then the 13th NSString
in the array would become the UILabel
's text.
例如,如果用户单击第 13 个UICollectionViewCell
的按钮,则NSString
数组中的第 13 个将成为UILabel
的文本。
What I have done:
我做了什么:
I have made my own subclass of UICollectionViewCell
for the nib file that I use for all of the UICollectionViewCell
s, & connected the button to the .h file as a IBAction
. I have also imported the MainViewController.h
, which is the one that contains the array property that stores the NSString
s.
我UICollectionViewCell
为用于所有UICollectionViewCell
s的 nib 文件创建了自己的子类,并将按钮作为 .h 文件连接到 .h 文件IBAction
。我还导入了MainViewController.h
,它包含存储NSString
s的数组属性。
When I edit the code in the UICollectionViewCell
's action, I cannot access the array property. The button does work - I placed an NSLog in the IBAction
's method, which does work.
当我在UICollectionViewCell
的操作中编辑代码时,我无法访问数组属性。该按钮确实有效 - 我在IBAction
的方法中放置了一个 NSLog ,它确实有效。
I have searched through tens of other answers on SO, but none answer my specific question. I can update this with samples of my code if requested.
我在 SO 上搜索了数十个其他答案,但没有一个回答我的具体问题。如果需要,我可以用我的代码示例更新它。
回答by Anupdas
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.
我为我用于所有 UICollectionViewCells 的 nib 文件创建了自己的 UICollectionViewCell 子类,并将按钮连接到 .h 文件作为 IBAction。
If you connect the IBAction to the subclass of collectionViewCell you would need to create a delegate to make the touch event available in the viewController where you are displaying the data.
如果您将 IBAction 连接到 collectionViewCell 的子类,您将需要创建一个委托以使触摸事件在您显示数据的 viewController 中可用。
One easy tweak is to add the button the collectionViewCell, connect it's IBOutlet to the cell. But not IBAction. In the cellForRowAtIndexPath:
add an eventHandler for button in that viewController containing collectionView.
一个简单的调整是添加 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;
}
回答by Anil Varghese
Please try like this..
请尝试这样..
In YourCollectionViewCell.h
在 YourCollectionViewCell.h 中
Create an IBOutlet not IBAction called button for the UIButton that you added to the xib. Remember you should connect the outlet to the cell object not to the file owner in the xib.
为您添加到 xib 的 UIButton 创建一个 IBOutlet 而不是 IBAction 调用按钮。请记住,您应该将插座连接到单元格对象,而不是连接到 xib 中的文件所有者。
MainViewController.m
主视图控制器.m
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
cell.button.tag = indexPath.row;
[cell.button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
-(void)buttonPressed:(UIButton*)sender
{
NSLog(@"%d : %@",sender.tag,[array objectAtIndex:sender.tag]);
self.textLabel.text = [array objectAtIndex:sender.tag];
}
Edit- Handle multiple sections
编辑 - 处理多个部分
-(void)buttonPressed:(UIButton*)sender
{
NSIndexPath *indexPath = [self.collectionView indexPathForCell: (UICollectionViewCell *)sender.superview.superview];
NSLog(@"Section : %d Row: %d",indexPath.section,indexPath.row);
if (0 == indexPath.section) {
self.textLabel.text = [firstArray objectAtIndex:indexPath.row];
}
else if(1 == indexPath.section)
{
self.textLabel.text = [secondArray objectAtIndex:indexPath.row];
}
}
回答by matt
When I edit the code in the UICollectionViewCell's action, I cannot access the array property.
当我在 UICollectionViewCell 的操作中编辑代码时,我无法访问数组属性。
That's because you connected the button action to the "wrong" object. It needs to be connected to the MainViewController (or whoever it is that doeshave access to the array property).
那是因为您将按钮操作连接到“错误”对象。它需要被连接到MainViewController(或谁它是确实有权访问数组属性)。
You are going to have several tasks to perform:
您将有几项任务要执行:
Receive the button action message.
Access the array (the modelfor the data).
Throw a switch saying which cell should now have its label showing.
Tell the collection view to
reloadData
, thus refreshing the cells.
接收按钮动作消息。
访问数组(数据模型)。
抛出一个开关,说明哪个单元格现在应该显示其标签。
告诉集合视图
reloadData
,从而刷新单元格。
All those tasks should most conveniently belong to oneobject. I am presuming that this is MainViewController (and thus I am presuming that MainViewController is the delegate/datasource of the collection view).
所有这些任务应该最方便地属于一个对象。我假设这是 MainViewController (因此我假设 MainViewController 是集合视图的委托/数据源)。