ios - 无法访问 Collection View 单元格信息

标签 ios objective-c xcode uicollectionview uicollectionviewcell

您好,我正在尝试通过 UIsegmentedcontrol 访问 UICollection View 单元格信息。

在 Collection View 中,我有四个标签和 UIsegmented 控件。当我点击分段控件时,我想显示标签值。

这是我的代码。

- (UIView *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
cell.mySegmentedControl.tag = indexPath.row;
selectedSegment = cell.mySegmentedControl.selectedSegmentIndex;
[cell.mySegmentedControl addTarget:self action:@selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged];
cell.caseid.text=[tmpDict objectForKey:@"CaseId"];
caseid = [tmpDict objectForKey:@"CaseId"];
}

- (void) segmentValueChanged: (UISegmentedControl *) sender {
//NSInteger index = sender.tag;
if(sender.selectedSegmentIndex == 0)
{
 NSString *localcaseid = caseid; // it shows default value may be first cell value.
CollectionViewCell * cell = [[CollectionViewCell alloc]init];
NSString *localcaseid = cell.caseid.text; //it prints null value
}
else
{
}

上面的代码对我不起作用。任何帮助将不胜感激。我想显示该特定单元格的信息。

最佳答案

    if(sender.selectedSegmentIndex == 0) {
        NSString *localcaseid = caseid;
        NSIndexPath *tempIndexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
        CollectionViewCell *cell = (CollectionViewCell *)[CollectionView cellForItemAtIndexPath:tempIndexPath];
        NSString *localcaseid = cell.caseid.text;
        NSLog(@"%@",localcaseid);
    }
    else {
    }

关于ios - 无法访问 Collection View 单元格信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40236937/

相关文章:

ios - 切换 View Controller 后第二次调用方法的问题

ios - 如何使用带有 AFNetworking 的 POST 将图像作为参数(参数)发送

ios - Swift 预期声明错误?

ios - Apple Watch 设置背景图片

ios - 无法在此文件中预览 - 事件方案未构建此文件 : SwiftUI on Xcode 11 in CatalinaOS

ios - 如何让我的角色 "ride"与移动平台一起移动?

xcode - 类没有初始值设定项 (Xcode 6.3)

ios - XCode 4.3, "Cannot define category for undefined class"使用多个 header

iphone - 改进 UISearchDisplayController 中的搜索

objective-c - 将 "SELF IN"与 NSPredicate 和 RLMResults 一起使用