如何以不同的方法访问 uicollectionviewcell 对象?
How to access uicollectionviewcell objects in different methods?
我有一个集合视图,我子class编辑了集合视图单元格,所以我有重复的内容。
在我的集合视图 class 中,我使用此方法访问和设置单元格中的对象,例如设置附加到单元格的标签?
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
//Configure Cell
//Set Name
DataManager *dm = [DataManager sharedInstance];
NSMutableArray *names = [dm objectForKey:@"nameArray"];
NSString *name = [names objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@", name]; //set cell here
return cell;
}
但是,我需要更新cell.textLabel
,但我无法通过其他方法访问cell.textLabel
?
如何使用除此之外的其他方法访问 cell.textLabel
?
*注意,我想更新我所有的单元格,而不仅仅是一个单元格。
所以我建议您保留一个数组,其中包含您要更改的单元格的索引路径,然后每次您想要更改文本时调用一个方法并更改它们。这是我每次只更改一个单元格的方法。
-(void) makePriorCell:(NSIndexPath *) indexPath{
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
//cell.backgroundColor = [UIColor greenColor];
UILabel *label = (UILabel *)[cell viewWithTag:101];
if (indexPath.item==0) {
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:@"2A copy.png"];
}
else if (indexPath.item==1) {
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:@"3A copy.png"];
}
else if (indexPath.item==2) {
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:@"4A copy.png"];
}
else if (indexPath.item==3) {
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:@"5A copy.png"];
}
else if (indexPath.item==4) {
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:@"5a_Istoria_Perivallon copy.png"];
}
else if (indexPath.item==5) {
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:@"7A copy.png"];
}
else if (indexPath.item==6) {
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:@"6A copy.png"];
}
else if (indexPath.item==7) {
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:@"8A copy.png"];
}
else if (indexPath.item==8) {
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:@"9A copy.png"];
}
else if (indexPath.item==9) {
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:@"10A copy.png"];
}
else if (indexPath.item==10) {
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:@"11A copy.png"];
}
else if (indexPath.item==11) {
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:@"12A copy.png"];
}
label.textColor = [UIColor blackColor];
cell.backgroundColor=[UIColor whiteColor];}
出于我的目的,我更改了指定单元格的图像、文本颜色和单元格背景。如果上面有标签,您可以对所需单元格的文本执行相同的操作。
您只能使用 visibleCells
方法访问 "visible" UICollectionViewCell
。
对于那些 "invisible" 个单元格,您必须在 collectionView:cellForItemAtIndexPath
中配置它们。
你的 CollectionViewCell
只是视图,你应该从模型中给它信息并相应地更新,就像你在 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
中所做的那样
我当时的建议是先更新您的模型,然后再更新您的单元格:
dm[@"nameArray"] = updated_info //Updated names
[DataManager sharedInstance] = ...//Update DataManager
collectionView.reloadData //Update all your cells
另外,在 cellForItemAtIndexPath: 方法中调用 DataManager *dm = [DataManager sharedInstance];
时要小心。也许为此使用依赖注入会更好。你可以看到更多相关信息 here
我有一个集合视图,我子class编辑了集合视图单元格,所以我有重复的内容。
在我的集合视图 class 中,我使用此方法访问和设置单元格中的对象,例如设置附加到单元格的标签?
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
//Configure Cell
//Set Name
DataManager *dm = [DataManager sharedInstance];
NSMutableArray *names = [dm objectForKey:@"nameArray"];
NSString *name = [names objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@", name]; //set cell here
return cell;
}
但是,我需要更新cell.textLabel
,但我无法通过其他方法访问cell.textLabel
?
如何使用除此之外的其他方法访问 cell.textLabel
?
*注意,我想更新我所有的单元格,而不仅仅是一个单元格。
所以我建议您保留一个数组,其中包含您要更改的单元格的索引路径,然后每次您想要更改文本时调用一个方法并更改它们。这是我每次只更改一个单元格的方法。
-(void) makePriorCell:(NSIndexPath *) indexPath{
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
//cell.backgroundColor = [UIColor greenColor];
UILabel *label = (UILabel *)[cell viewWithTag:101];
if (indexPath.item==0) {
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:@"2A copy.png"];
}
else if (indexPath.item==1) {
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:@"3A copy.png"];
}
else if (indexPath.item==2) {
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:@"4A copy.png"];
}
else if (indexPath.item==3) {
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:@"5A copy.png"];
}
else if (indexPath.item==4) {
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:@"5a_Istoria_Perivallon copy.png"];
}
else if (indexPath.item==5) {
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:@"7A copy.png"];
}
else if (indexPath.item==6) {
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:@"6A copy.png"];
}
else if (indexPath.item==7) {
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:@"8A copy.png"];
}
else if (indexPath.item==8) {
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:@"9A copy.png"];
}
else if (indexPath.item==9) {
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:@"10A copy.png"];
}
else if (indexPath.item==10) {
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:@"11A copy.png"];
}
else if (indexPath.item==11) {
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:@"12A copy.png"];
}
label.textColor = [UIColor blackColor];
cell.backgroundColor=[UIColor whiteColor];}
出于我的目的,我更改了指定单元格的图像、文本颜色和单元格背景。如果上面有标签,您可以对所需单元格的文本执行相同的操作。
您只能使用 visibleCells
方法访问 "visible" UICollectionViewCell
。
对于那些 "invisible" 个单元格,您必须在 collectionView:cellForItemAtIndexPath
中配置它们。
你的 CollectionViewCell
只是视图,你应该从模型中给它信息并相应地更新,就像你在 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
我当时的建议是先更新您的模型,然后再更新您的单元格:
dm[@"nameArray"] = updated_info //Updated names
[DataManager sharedInstance] = ...//Update DataManager
collectionView.reloadData //Update all your cells
另外,在 cellForItemAtIndexPath: 方法中调用 DataManager *dm = [DataManager sharedInstance];
时要小心。也许为此使用依赖注入会更好。你可以看到更多相关信息 here