无法在我的自定义集合视图单元格中引用按钮

时间:2013-11-26 00:26:57

标签: ios objective-c uibutton uicollectionview uicollectionviewcell

使用集合视图,我创建了自己的自定义视图单元格并将其链接到自己的xib文件。

在我创建单元格时的cellForItemAtIndexPath方法中,我可以引用单元格中的一些标签和文本字段,但是我遇到的问题是我无法引用其他一些按钮(更改它们的字体或添加目标到他们)

我不知道是什么原因但是当我尝试NSLog myButton.titleLabel.text的值时,它总是打印为空。

    NSMutableArray *data = [self.dataArray objectAtIndex:cv.tag];
    NaaiCell *cell = (NaaiCell *)[_naaiCollectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier
                                                                                forIndexPath:indexPath];
    Naai* cellData = (Naai*)[data objectAtIndex:indexPath.row];

    //these values works fine for me and I can reference to them.

    [cell.naaiId setText:cellData.naaiId];
    cell.name.font = font;
    [cell.name setText:[c convertArabic:cellData.deceasedName]];

    [cell.text setText:[c convertArabic:cellData.text]];
    cell.text.font = sFont ;
    cell.text.textAlignment = NSTextAlignmentRight;
    [cell.text setTextColor:[UIColor grayColor]];


    //the problem with these three buttons.

    cell.btnAazi.titleLabel.font = [UIFont fontWithName:@"Droid Arabic Kufi" size:10];
    cell.btnMushatra.titleLabel.font = [UIFont fontWithName:@"Droid Arabic Kufi" size:10];
    cell.btnBarkyah.titleLabel.font = [UIFont fontWithName:@"Droid Arabic Kufi" size:10];


    [cell.btnAazi addTarget:self action:@selector(azziOnTheFly:) forControlEvents:UIControlEventTouchUpInside];
    genericCell = cell;

2 个答案:

答案 0 :(得分:0)

在自定义单元格的xib文件中,确保单元格重用标识符的值与您在此代码中指定的值相同:

NaaiCell *cell = (NaaiCell *)[_naaiCollectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier
                                                                            forIndexPath:indexPath];

我猜这是以某种方式使用标准集合视图单元格。

答案 1 :(得分:0)

经过@rdelmar的调试时间后,他终于达到了这个目的:即使我通过将它们拖到.h文件来连接插座,但是当我转到XIB文件然后 - >显示连接检查器点击我发现插座没有连接! 我连接它们,每件事都很有效:) 再次感谢@rdelmar:)

enter image description here