Xcode:从数据库设备获取数据

时间:2014-01-20 03:31:38

标签: ios nsarray uicollectionview

我是初学IOS开发人员请引导我一些关于'我怎样才能从数据库中获取数据' 我有2个课程MainViewControllerBookViewController 这个工作正常,离线模式只能显示数据。

MainViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    db = [[DBManager alloc]init];
    [db initWithDB];
    bookArray = [db getBookData];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    _databaseVersion = [[bookArray objectAtIndex:indexPath.item]bookVersion];

    NSLog(@"%f",_databaseVersion);
    BookCell *bookcell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ReuseCell" forIndexPath:indexPath];

    return bookcell;

}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [bookArray count];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

MainViewController正在运作 但是当我将这样的数据发送到BookViewController

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{


        if ([segue.identifier isEqualToString:@"course1"]){
            BookViewController *controller = (BookViewController *)segue.destinationViewController;

            [controller setBookArray:bookArray];
            controller.checkType = @"1";
            controller.statusMode = statusMode;
            controller.databaseVersion = _databaseVersion;

        }
}

但bookArray不适用于BookViewController

BookViewController.m

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);


    BookCell *bookcell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ReuseCell" forIndexPath:indexPath];
    BookCell *cell = (BookCell *)[collectionView cellForItemAtIndexPath:indexPath];
     NSInteger row= indexPath.row;

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            NSURL *imageURL = [NSURL URLWithString:[[bookList objectAtIndex:indexPath.item]coverURL]];

            NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
            UIImage *image = [UIImage imageWithData:imageData];

            dispatch_async(dispatch_get_main_queue(), ^{


                bookcell.bookCover.image = [UIImage imageWithData:imageData];
                bookcell.bookCover.alpha = 0.5f;
                bookcell.bookDetailLabel.hidden = NO;
                        NSLog(@"Row == %d ",row);

         if ([[bookList objectAtIndex:indexPath.row]bookVersion] > [[bookArray   objectAtIndex:indexPath.row]bookVersion] && [[bookArray objectAtIndex:indexPath.row]bookVersion] > 0) {
           bookcell.bookCover.image = [UIImage imageWithData:imageData];
           bookcell.bookCover.alpha = 1.0f;
           bookcell.bookDetailLabel.hidden = NO;
           bookcell.bookDetailLabel.text = @"Update";


          }

                });
        });

    }
    return bookcell;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [bookList count];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//bookList is data from Webservice and bookArray is data from database.

但我不知道为什么我不能使用数据库中的数据 我在[[bookArray objectAtIndex:indexPath.item]bookVersion]; cellForItemAtIndexPath: BookViewController.m尝试这个[[bookArray objectAtIndex:0]bookVersion];,但是bookArray总是崩溃因为bookArry只有一个数据可行,如果我这样做 if([[bookList objectAtIndex:indexPath.row]bookVersion] > [[bookArray objectAtIndex:indexPath.row]bookVersion]) { .... } 将得到一个数据和没有崩溃,但我想使用

[[bookList objectAtIndex:indexPath.row]bookVersion]

但是这一次总是崩溃,因为[[bookArray objectAtIndex:indexPath.row]bookVersion]在webservice上有10个数据,但[[bookArray objectAtIndex:indexPath.row]bookVersion]只有一个数据请帮我知道如何获取null数据,如果不这样做就不会崩溃有数据会显示[[bookArray objectAtIndex:indexPath.item]bookVersion]请请。

NSString *testData = [[bookArray objectAtIndex:indexPath.item]downloadFlag]; NSLog(@"test flag %@",testData); 2014-01-20 11:14:29.837 GreenAcademy PowerShelf[596:60b] test flag 1 2014-01-20 11:14:29.842 GreenAcademy PowerShelf[596:60b] ------------------------- 2014-01-20 11:14:29.844 GreenAcademy PowerShelf[596:60b] test flag 1 2014-01-20 11:14:29.849 GreenAcademy PowerShelf[596:60b] ------------------------- 2014-01-20 11:14:29.851 GreenAcademy PowerShelf[596:60b] test flag 1 2014-01-20 11:14:29.854 GreenAcademy PowerShelf[596:60b] ------------------------- 2014-01-20 11:14:29.863 GreenAcademy PowerShelf[596:60b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 2]' *** First throw call stack: (0x2e349f4b 0x38a1b6af 0x2e280533 0xb6911 0x30b1de65 0x30b1c5d9 0x30b18921 0x30abcda3 0x30743c6b 0x3073f47b 0x3073f30d 0x3073ed1f 0x3073eb2f 0x3073885d 0x2e3151cd 0x2e312b71 0x2e312eb3 0x2e27dc27 0x2e27da0b 0x32f51283 0x30b21049 0x80b7d 0x38f23ab7) libc++abi.dylib: terminating with uncaught exception of type NSException

崩溃
{{1}}

1 个答案:

答案 0 :(得分:2)

如果您不希望代码在尝试访问项目数组之外的项目时生成异常,请首先检查count属性。因此,而不是:

NSInteger row = indexPath.row
if ([[bookList objectAtIndex:row] bookVersion] > [[bookArray objectAtIndex:row] bookVersion])
{ .... }

您可以先添加count属性的检查,首先:

if ([bookArray count] >= (row + 1) && [[bookList objectAtIndex:row] bookVersion] > [[bookArray objectAtIndex:row] bookVersion])
{ .... }

或者,或许更简洁一点,使用新的数组下标符号:

if ([bookArray count] >= (row + 1) && [bookList[row] bookVersion] > [bookArray[row] bookVersion])
{ .... }