ios - 代码 :Get Data From Database device

标签 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是来自Webservice的数据,bookArray是来自数据库的数据。

但是我不知道为什么我不能使用数据库中的数据 我尝试这样 [[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] 在网络服务中有 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])
{ .... }

关于ios - 代码 :Get Data From Database device,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21225807/

相关文章:

ios - 如何在 iOS 应用程序中计算一段时间内的 Parse API 请求?

ios - 创建 XML 时如何有效检查 NSDictionary 键是否有值

ios - 将数组内容写入文件

ios - 使 CollectionView Cell 水平单元格比其他单元格大

ios - Swift:如何通过 Storyboard而不是使用 XLPagerTabStrip 以编程方式显示 subview Controller

ios - 如何优雅地停止react-native run-ios?

arrays - 如何求 NSMutableArray 所有元素的总和? swift

ios - Collection View 单元格内的 MKMapview 不可见

ios - 在 secondViewController 中有 4 个 textField 。我想用 firstviewController Collection View 单元检查这个 textField 值

ios - iOS 12-NSFaceIDUsageDescription突然不起作用