无法访问集合视图单元格信息
Unable to access UICollection view cell infromation
您好,我正在尝试通过 UIsegmentedcontrol 访问 UICollection 视图单元格信息。
在集合视图中,我有四个标签和 UIsegmented control.When我点击分段控件,我想显示标签值。
这是我的代码。
- (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
{
}
以上代码不适用于 me.any 帮助 appreciated.I 想要显示该特定单元格的信息。
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 {
}
// 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中的delegate分配给你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];
}
您好,我正在尝试通过 UIsegmentedcontrol 访问 UICollection 视图单元格信息。
在集合视图中,我有四个标签和 UIsegmented control.When我点击分段控件,我想显示标签值。
这是我的代码。
- (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
{
}
以上代码不适用于 me.any 帮助 appreciated.I 想要显示该特定单元格的信息。
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 {
}
// 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中的delegate分配给你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];
}