我是IOS开发人员初学者,请向我介绍“如何仅从数据库中获取数据”
我为此MainViewControllerBookViewController有2个班级
这是一个很好的脱机模式可以显示我只有数据的工作。

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总是崩溃,因为如果我这样做,bookArry只有一个数据是有效的[[bookArray objectAtIndex:0]bookVersion];将获得一个数据而不是崩溃,但是我想使用

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


但这总是崩溃,因为[[bookList objectAtIndex:indexPath.row]bookVersion]在Web服务中有10个数据,但是[[bookArray objectAtIndex:indexPath.row]bookVersion]只有一个数据。请帮助我如何获取[[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

最佳答案

如果您不希望代码在尝试访问超出项数组的项时生成异常,请首先检查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])
{ .... }

10-08 08:59
查看更多