我面临着获取uicollectionViewCell文本的问题。 当我选择一个特定的细胞。 我正在使用各种代码,但没有人对我有用。 所以,请帮我解决这个问题。
答案 0 :(得分:0)
如果您使用默认的UICollectionView
课程,那么您可以使用这行代码。
UICollectionView *cell //use alloc etc
cell.textLabel.text //this line return the text of collectionviewcell. as in UITableView
答案 1 :(得分:0)
我相信您必须创建自定义UICollectionViewCell
类...
使用UICollectionView
的一种简单方法是创建一个日历月视图,其中包含多个UICollectionViewCells
作为月视图中的不同日期。
下面是UICollectionViewCell
与UICollectionView
使用的基本实现:
CBCalendarCell.h
#import <UIKit/UIKit.h>
@interface CBCalendarCell : UICollectionViewCell
@property UIView *circleView;
@property UIView *selectedView;
@property UILabel *dateLabel;
@property UIView *eventView;
@end
CBCalendarCell.m
#import "CBCalendarCell.h"
#import "CBConstants.h"
@implementation CBCalendarCell
- (id)initWithFrame:(CGRect)frame
{
if (!(self = [super initWithFrame:frame])) return nil;
self.backgroundColor = cellBackgroundColor;
self.dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height * labelToCellRatio)];
self.dateLabel.textAlignment = NSTextAlignmentCenter;
self.eventView = [[UIView alloc] initWithFrame:CGRectMake(self.dateLabel.frame.size.width / 2.0f - 1.5f,
self.dateLabel.frame.origin.y + self.dateLabel.frame.size.height - 5,
3, 3)];
[[self.eventView layer] setCornerRadius:1.5f];
[self.eventView setBackgroundColor:cellEventViewColor];
CGRect labelFrame = self.dateLabel.frame;
CGSize labelSize = labelFrame.size;
CGPoint origin;
int length;
if (labelSize.width > labelSize.height) {
origin.x = (labelSize.width - labelSize.height * circleToCellRatio) / 2;
origin.y = (labelSize.height * (1 - circleToCellRatio)) / 2;
length = labelSize.height * circleToCellRatio;
}
else{
origin.x = (labelSize.width * (1 - circleToCellRatio)) / 2;
origin.y = (labelSize.height - labelSize.width * circleToCellRatio) / 2;
length = labelSize.width * circleToCellRatio;
}
self.circleView = [[UIView alloc] initWithFrame:CGRectMake(origin.x, origin.y, length, length)];
self.circleView.layer.cornerRadius = length / 2;
self.circleView.backgroundColor = currentDateCircleColor;
self.selectedView = [[UIView alloc] initWithFrame:CGRectMake(origin.x, origin.y, length, length)];
self.selectedView.layer.cornerRadius = length / 2;
self.selectedView.backgroundColor = selectedDateCircleColor;
[self.viewForBaselineLayout addSubview:self.circleView];
[self.viewForBaselineLayout addSubview:self.selectedView];
[self.viewForBaselineLayout addSubview:self.dateLabel];
[self.viewForBaselineLayout addSubview:self.eventView];
return self;
}
@end
在UICollectionView中注册自定义UICollectionViewCell,如下所示:
// 'calendar' is the UICollectionView instance name
[calendar registerClass:[CBCalendarCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
然后在- (UICollectionViewCell *)collectionView:cellForItemAtIndexPath:
中配置单元格:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CBCalendarCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
...
...
cell.circleView.hidden = YES;
cell.selectedView.hidden = NO;
cell.dateLabel.text = @"Text";
cell.eventView.hidden = NO;
...
...
return cell;
}
此代码来自SACalendar存储库... Nop Shusang保留所有权利