您好我正在尝试通过UIsegmentedcontrol访问UICollection视图单元格信息。
在集合视图中,我有四个标签和UI分段控件。当我点击分段控件时,我想显示标签值。
这是我的代码。
- (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
{
}
以上代码对我不起作用。我们将不胜感激。我想显示该特定单元格的信息。
答案 0 :(得分:1)
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 {
}
答案 1 :(得分:1)
// CustomCell.h
#import <UIKit/UIKit.h>
@protocol CollectionViewCellDelegate;
@interface CustomCollectionViewCell : UICollectionViewCell
@property(weak, nonatomic) id<CollectionViewCellDelegate> delegate;
@end
// CustomCell.m
#import "CollectionViewCell.h"
// Create your delegate
@protocol CollectionViewCellDelegate <NSObject>
- (void)collectionViewCell:(CustomCollectionViewCell *)cell segmentedControlChangedValue:(UISegmentedControl *)control;
@end
@implementation CustomCollectionViewCell
// Implement this delegate call on the cell, not on the viewController
- (void) segmentValueChanged: (UISegmentedControl *) sender {
// Call your delegate with the cell added as parameter
[self.delegate collectionViewCell:self segmentedControlChangedValue:sender];
}
@end
在viewcontroller中,确保它向代理确认,在viewController声明旁边添加<CollectionViewCellDelegate>
并导入单元头文件。
// ViewController.h
#import "CustomCollectionViewCell.h"
@interface ViewController : UIViewController <CollectionViewCellDelegate>
// ViewController.m
//将cellForItem中的委托分配给VC
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
// create your cell
// assign your cell's delegate to self
cell.delegate = self
}
// Declare the delegate method on the viewController
- (void)collectionViewCell:(CustomCollectionViewCell *)cell segmentedControlChangedValue:(UISegmentedControl *)control {
// here you now have access to the cell, where the segmented was pressed in
// Do what you need to and make sure you reload the data at the end of this function, when you have set up your cell
[self.collectionView reloadData];
}