如何以不同的方法访问uicollectionviewcell对象?

时间:2015-07-10 12:31:54

标签: ios objective-c uicollectionview uicollectionviewcell

我有一个集合视图,我将集合视图单元格子类化,因此我有重复的内容。

在我的集合视图类中,我使用此方法来访问和设置单元格中的对象,例如设置附加到单元格的标签?

- (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

*注意,我想更新所有细胞,而不仅仅是一个细胞。

3 个答案:

答案 0 :(得分:0)

所以我建议您保留一个Array,其中包含要更改的单元格的indexPaths,然后每次要更改文本时调用方法并更改它们。这是我用来每次只更改一个单元格的方法。

-(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];}

为了我的目的,我改变指定单元格的图像,文本颜色和单元格的背景。如果你有一个标签,你可以对你想要的单元格的文本做同样的事。

答案 1 :(得分:0)

您只能访问"可见" UICollectionViewCell,使用visibleCells方法。

对于那些"隐形"单元格,您必须在collectionView:cellForItemAtIndexPath

中配置它们

答案 2 :(得分:0)

您的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

的更多信息