UICollectionViewCell NSMutableArray上的错误访问

时间:2014-03-25 16:21:48

标签: cocoa-touch nsmutablearray uicollectionview exc-bad-access uicollectionviewcell

我在应用程序中使用UICollectionView,其中集合视图的单元格基于来自服务器的数据。

当应用程序加载时,我从服务器下载数据并将其转换为存储在NSMutableArray中的单个NSObject

@property (nonatomic, retain) NSMutableArray *dataArray;

然后使用此dataArray的计数来确定集合视图中的单元格数。

当我尝试在cellForItemAtIndexPath集合视图委托方法中访问此dataArray时出现问题:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"ReminderCard" forIndexPath:indexPath];

    GMReminderCard *reminderCard = (GMReminderCard *)cell;
    GMReminder *rem = [self.dataArray objectAtIndex:[indexPath row]];

    [reminderCard initialiseReminder:rem];

    cell.layer.cornerRadius     = 5;
    cell.layer.masksToBounds    = YES;
    return cell;
}


- (void)initialiseReminder:(GMReminder *)rem
{
    if(!loan)return;

    _thisReminder = rem;


    NSURL *imageURL = [NSURL URLWithString:_thisReminder.image relativeToURL:[NSURL    URLWithString:@"http://host.cloudfront.net/"]];

    [self.image setImageWithURL:[imageURL absoluteURL]
           placeholderImage:[UIImage imageNamed:@"973-user.png"]];

    [_title setText:[_thisReminder title]];
    [_description setText:[_thisReminder description]];
}

这是我从服务器获取数据的代码,返回的NSMutableArray包含GMReminder对象:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self becomeFirstResponder];

    dataArray = [[NSMutableArray alloc] init];

    [GMReminders getCurrentReminders:^(NSMutableArray *rems, NSError *err){
        if (!err) {
            dataArray = [[NSMutableArray alloc] initWithArray:rems];
            [collectionView reloadData];
        }
    }];

    [self.view setBackgroundColor:[UIColor blackColor]];

    [self buildBackgroundSlider];
    [self buildLabel];
    [self buildCollectionView];
}

最初单元格按预期加载,但是当我尝试滚动集合视图时,第一次尝试使用GMReminder变量时会出现错误的访问错误。

非常感谢任何帮助

2 个答案:

答案 0 :(得分:0)

在ViewDidload方法中以编程方式make collectionview: -

UICollectionView *collection_view=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 320, [UIScreen mainScreen].bounds.size.height) collectionViewLayout:layout];
    [collection_view setBackgroundColor:[UIColor colorWithRed:0.810746 green:0.764681 blue:0.764681 alpha:0.8]];
    collection_view.delegate = self;
    collection_view.dataSource = self;
    [collection_view registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
    [self.view addSubview:collection_view];

为UicollectionView的单元格定义委托: -

- (UICollectionViewCell *) collectionView:(UICollectionView *) collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier=@"cellIdentifier";

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    if (!cell)
    {
        cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(1,0,cell.frame.size.width, cell.frame.size.height)];
    }
    return cell;
}

答案 1 :(得分:0)

替换此

dataArray = [[NSMutableArray alloc] initWithArray:rems];

dataArray = [[NSMutableArray alloc] initWithArray:[rems copy]];