Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Xcode:从数据库设备获取数据_Ios_Nsarray_Uicollectionview - Fatal编程技术网

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

Ios Xcode:从数据库设备获取数据,ios,nsarray,uicollectionview,Ios,Nsarray,Uicollectionview,我是IOS开发新手,请指导我如何从只有我自己的数据库中获取数据 对于这个MainViewController和BookViewController 这是一个工作良好的离线模式可以显示数据只有我有 MainViewController.m - (void)viewDidLoad { [super viewDidLoad]; db = [[DBManager alloc]init]; [db initWithDB]; bookArray = [db getBookDa

我是IOS开发新手,请指导我如何从只有我自己的数据库中获取数据 对于这个
MainViewController
BookViewController
这是一个工作良好的离线模式可以显示数据只有我有

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;
}
-(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;
}
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

-(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;

        }
}
BookViewController.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;
}
-(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:indexath.item]bookVersion]
CellForItemAtIndex路径中:
BookViewController.m
上,但是bookArray总是崩溃,因为如果我这样做,bookArry只有一个数据是工作的
[[bookArray objectAtIndex:0]bookVersion]将获得一个数据,不会崩溃,但我想使用

    if([[bookList objectAtIndex:indexPath.row]bookVersion] > [[bookArray objectAtIndex:indexPath.row]bookVersion])
{ .... }
但是这一次总是会崩溃,因为有
[[booklistobjectatindex:indexath.row]bookVersion]
在Web服务中有10个数据,但是
[[bookArray objectAtIndex:indexath.row]bookVersion]
只有一个数据请帮助我如何获取
[[bookArray objectAtIndex:indexath.row]bookVersion]
data和not crash if not have data将显示
null

[[bookArray objectAtIndex:indexath.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])
{ .... }

不过,您在
BookViewController.m
中的
cellForItemAtIndexPath
非常奇怪。您正在引用shelfcell,但请不要向我们显示。在
dequeueReusableCellWithReuseIdentifier
之后,您使用
cellForItemAtIndexPath
设置
cell
(我猜这应该是
shelfcell
),但这种构造没有意义,除非您在块内这样做,以便从后台队列调度回主队列。也不清楚您为什么在后台队列中调用
[db initWithDB]
。我正在编辑我的问题,请帮助我@罗布:非常感谢你,我写错了。谢谢你的好意,我真是感激不尽。这太好了。谢谢老师。请再帮我一次。为什么如果我不先下载bookID=3,就不能显示bookCover bookID=4。我使用此代码来显示一些书籍
if([bookArray count]>=(row+1)和&[[bookArray objectAtIndex:row]bookVersion]>[[bookArray objectAtIndex:row]bookVersion]&[[bookArray objectAtIndex:row]bookID]IsequalString:[[bookArray objectAtIndex:row]bookID]]){..
请指导我。@user2180698很难说,因为还不完全清楚你的应用程序的“模型”是什么(无意冒犯,你的问题很混乱,包括很多不相关的东西,但讽刺的是,这些东西还不足以回答这些问题;而且已经是一团糟,我不太愿意请你添加更多代码;哈哈)“如何协调这两个单独的数组”,正确的答案可能是“不应该有两个单独的数组”“。要么有一个随操作而更新的结构,要么第二个结构应该是由图书id键入的字典,而不是数组。无论采用哪种方法,您都可以以非顺序的方式更新记录,而且效果良好。但是,当您以顺序方式添加和查询项目时,数组确实工作得最好。非常感谢您,好的,我会试试。再次感谢你的代码是非常有用的。