Ios 使用UICollectionViewCell上的按钮显示阵列中的数据
我有一个Ios 使用UICollectionViewCell上的按钮显示阵列中的数据,ios,objective-c,xcode,ios6,uicollectionview,Ios,Objective C,Xcode,Ios6,Uicollectionview,我有一个NSStrings数组,一个UILabel&一个UICollectionView 我的问题: 我希望数组的计数确定有多少个UICollectionViewCell 每个UICollectionViewCell都包含一个按钮。单击后,我希望此按钮使数组中与UICollectionViewCell编号对应的数据显示在标签中 例如,如果用户单击第13个UICollectionViewCell按钮,则数组中的第13个NSString将成为UILabel的文本 我所做的: 我为用于所有UIColl
NSStrings
数组,一个UILabel
&一个UICollectionView
我的问题:
我希望数组的计数确定有多少个UICollectionViewCell
每个UICollectionViewCell
都包含一个按钮。单击后,我希望此按钮使数组中与UICollectionViewCell
编号对应的数据显示在标签中
例如,如果用户单击第13个UICollectionViewCell
按钮,则数组中的第13个NSString
将成为UILabel
的文本
我所做的:
我为用于所有UICollectionViewCell
的nib文件创建了自己的UICollectionViewCell
子类,并将按钮作为iAction
连接到.h文件。我还导入了MainViewController.h
,它包含存储NSString
s的数组属性
当我在UICollectionViewCell
的操作中编辑代码时,我无法访问数组属性。按钮不起作用-我在iAction
的方法中放置了一个NSLog,它确实起作用
我已经搜索了几十个其他答案,但没有一个回答我的具体问题。如果需要,我可以用我的代码样本来更新
在UICollectionViewCell的操作中编辑代码时,无法访问数组属性
这是因为您将按钮操作连接到了“错误”对象。它需要连接到MainViewController(或任何有权访问阵列属性的人)
您将有几个任务要执行:
- 接收按钮操作消息
- 访问阵列(数据的模型)
- 抛出一个开关,说明哪个单元格现在应该显示其标签
- 告诉集合视图重新加载数据,从而刷新单元格
单元格中,为包含collectionView的viewController中的按钮添加事件处理程序
- (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;
}
请像这样试试
在您的CollectionViewCell.h中
为添加到xib的UIButton创建一个名为button的IBOutlet而不是IBAction。请记住,应该将出口连接到单元对象,而不是连接到xib中的文件所有者
MainViewController.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];
}
-(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];
}
}
编辑-处理多个部分
- (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];
}
-(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];
}
}
按钮操作无法连接到MainViewController,因为它位于UICollectionViewCell内部。该标签实际上是MainViewController.xib的一部分,而不是UICollectionViewCell.nib(对不起,我应该指定)。假设UICollectionView中有50个UICollectionViewCell。整个集合视图中有50个按钮-每个单元格中有一个按钮。我想将数组的每个索引连接到每个单元格。我希望这有助于具体说明。我感谢你的帮助!“按钮操作无法连接到MainViewController”。当然可以。也许你不能通过在情节提要中绘制连接来设置它,但是你可以在代码中设置它(在你的collectionView:cellForItemAtIndexPath:
的实现中,这将是一个非常方便的地方,事实上现在有另一个答案向你展示了如何做到这一点——在这种情况下,你不必为自己做任何思考).当节也应使用时,您将如何处理?不可能处理多个节。。但这里OP将细节存储在一个数组中,并获取与单元号对应的适当数据。哪个实习生意味着他只有一个科。如果他有多个节,他就不能使用数组。我看到你的答案更一般。但我认为我的代码不够,我实际上有4个部分。您能解释一下为什么我不能使用数组吗?那么您将如何从数组中识别数据?您只有单元格号ie indexath.row。。对于每个部分,其计数类似于Sec1:0,1,2。。第0,1,2节。那么,如何使用数组索引识别数组中的数据属于哪个部分??不能使用单个数组。不同的部分需要不同的数组。因此,在你的工作中最好使用字典case@tagabek你可以很好地使用数组。你需要这样的格式。你应该有一系列字典。把每一本字典当作你的章节。每个节字典都有一个数组或行。所以它很容易使用。对这个问题有什么后续行动吗?我的主要问题是,我使用的单元格不是自定义UICollectionViewCell子类的实例。我使用的是常规UICollectionViewCell实例。新手犯的错误。谢谢你的帮助@tagabek也许应该接受Anupdas?