Collectionview学习(三)利用故事版动态改变cell内容展示实战demo
2017-03-27 10:35
465 查看
使用storyboard过程中遇到问题报错:the xxx outlet from xxx to the xxx is invalid. Outlet can't be connected to repeating content.
错误原因:在storyboar中自定义了cell,但是却在viewcontroller中索引了iboutlet 的cell内容。cell本身是不属于viewcontroller的,所以会报错。
解决方法:自定义cell,将iboutlet声明在自定义的cell中去。注意应当首先去除掉问题iboutlet之前的旧关联
![](https://oscdn.geek-share.com/Uploads/Images/Content/201703/1e339588554249203f4c18c85cec73b8)
因为当前的demo使用的是故事版的方式,在storyboard中已经设置了cell identifier,并且将cell的class设置为了自定义的cell名称。所以就不需要再次registerclass的操作了,否则会覆盖掉之前的cell而出错。
那么如果来达到访问故事版中的cell内容并且修改其内容呢?很简单,只要在对应的datasrouce方法中把创建的cell的类定义成自定义的类,并且在自定义的cell类中公开对应的接口就可以了。代码如下:
#import
<UIKit/UIKit.h>
@interface CustomCollectionviewCellCollectionViewCell :
UICollectionViewCell
@property (nonatomic,
strong)
IBOutlet
UILabel
*backgroundLabel;
@property (nonatomic,
strong)
IBOutlet
UIImage*backgroundImageView;
- (void)changeLabelBackgroundColor;
- (void)setTabelText:(NSString *)text;
@end
#import
"CustomCollectionviewCellCollectionViewCell.h"
@implementation CustomCollectionviewCellCollectionViewCell
- (void)changeLabelBackgroundColor {
self.backgroundLabel.backgroundColor = [UIColor greenColor];
- (void)setTabelText:(NSString
*)text {
self.backgroundLabel.text = text;
@end
#import
"CustomCollectionView.h"
#import
"CustomCollectionviewCellCollectionViewCell.h"
#define COLLECTIONVIEW_CELL_IDENTIFIER @"CollectionviewCellIdentifier"
@interface
CustomCollectionView ()
@property (nonatomic,
strong) IBOutlet
UICollectionView *collectionView;
@end
@implementation CustomCollectionView
- (void)viewDidLoad {
[super
viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 10;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (CustomCollectionviewCellCollectionViewCell
*)collectionView:(UICollectionView *)viewCollection cellForItemAtIndexPath:(nonnull
NSIndexPath *)indexPath {
CustomCollectionviewCellCollectionViewCell *cell = [viewCollection
dequeueReusableCellWithReuseIdentifier:COLLECTIONVIEW_CELL_IDENTIFIER
forIndexPath:indexPath];
[cell
changeLabelBackgroundColor];
[cell
setTabelText:[NSString
stringWithFormat:@"%@%ld",
@"Label", indexPath.row]];
return cell;
}
最终的效果图如下:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201703/432b901ccecd327f9f7431f8e652a3d7)
label的颜色得到了改变,文字信息也加上了。
错误原因:在storyboar中自定义了cell,但是却在viewcontroller中索引了iboutlet 的cell内容。cell本身是不属于viewcontroller的,所以会报错。
解决方法:自定义cell,将iboutlet声明在自定义的cell中去。注意应当首先去除掉问题iboutlet之前的旧关联
因为当前的demo使用的是故事版的方式,在storyboard中已经设置了cell identifier,并且将cell的class设置为了自定义的cell名称。所以就不需要再次registerclass的操作了,否则会覆盖掉之前的cell而出错。
那么如果来达到访问故事版中的cell内容并且修改其内容呢?很简单,只要在对应的datasrouce方法中把创建的cell的类定义成自定义的类,并且在自定义的cell类中公开对应的接口就可以了。代码如下:
#import
<UIKit/UIKit.h>
@interface CustomCollectionviewCellCollectionViewCell :
UICollectionViewCell
@property (nonatomic,
strong)
IBOutlet
UILabel
*backgroundLabel;
@property (nonatomic,
strong)
IBOutlet
UIImage*backgroundImageView;
- (void)changeLabelBackgroundColor;
- (void)setTabelText:(NSString *)text;
@end
#import
"CustomCollectionviewCellCollectionViewCell.h"
@implementation CustomCollectionviewCellCollectionViewCell
- (void)changeLabelBackgroundColor {
self.backgroundLabel.backgroundColor = [UIColor greenColor];
- (void)setTabelText:(NSString
*)text {
self.backgroundLabel.text = text;
@end
#import
"CustomCollectionView.h"
#import
"CustomCollectionviewCellCollectionViewCell.h"
#define COLLECTIONVIEW_CELL_IDENTIFIER @"CollectionviewCellIdentifier"
@interface
CustomCollectionView ()
@property (nonatomic,
strong) IBOutlet
UICollectionView *collectionView;
@end
@implementation CustomCollectionView
- (void)viewDidLoad {
[super
viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 10;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (CustomCollectionviewCellCollectionViewCell
*)collectionView:(UICollectionView *)viewCollection cellForItemAtIndexPath:(nonnull
NSIndexPath *)indexPath {
CustomCollectionviewCellCollectionViewCell *cell = [viewCollection
dequeueReusableCellWithReuseIdentifier:COLLECTIONVIEW_CELL_IDENTIFIER
forIndexPath:indexPath];
[cell
changeLabelBackgroundColor];
[cell
setTabelText:[NSString
stringWithFormat:@"%@%ld",
@"Label", indexPath.row]];
return cell;
}
最终的效果图如下:
label的颜色得到了改变,文字信息也加上了。
相关文章推荐
- Collectionview学习(三)利用故事版动态改变cell内容展示实战demo
- 【iOS知识学习】_iOS动态改变TableView Cell高度
- (转)iPhone实战:动态改变UITableView中的Cell高度
- ios开发笔记之如何让tableView根据文本内容动态改变cell的高度
- 【iOS知识学习】_iOS动态改变TableView Cell高度
- (转)iPhone实战:动态改变UITableView中的Cell高度
- Collectionview学习(三)不使用故事版,纯代码编写cell(cell也可从nib文件加载)
- 学习笔记一:UIImage初始化方法以及点击UICollectionView和遇到点击的TableViewCell
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- 利用reloadRowsAtIndexPaths:withRowAnimation:来动态改变cell的高度
- ios学习笔记: TableView利用label调整cell高度
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- UITableView动态改变Cell高度
- 动态改变tableview的cell的高度
- Android学习小demo(3)自定义ViewGroup 利用 scroller 实行屏幕滚动
- 动态改变UITableView中的Cell高度
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- IOS学习笔记(10)UITableView展示数据 cell删除 构建页眉页脚
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变