数组未填充新数据

发布于 2024-11-16 20:47:23 字数 2835 浏览 1 评论 0原文

在我的应用程序中,我使用数组来获取方法内的一些数据,但每次将对象添加到数组后,我都会检查其内容,然后总是显示数组内的 0 个对象,之后我重新加载表视图但什么也没有发生。没有事件被调用。我在这里显示我的代码:

在 .h 文件中

@interface ModalView:UIViewController

           <UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate> 

{

NSMutableArray *imageName;

}


@property (nonatomic,retain)NSMutableArray *imageName;

在 .m 文件中:-

@synthesize imageName;

      - (void)viewDidLoad {

       [super viewDidLoad];

     imageName=[[NSMutableArray alloc] init];

        [tableView1 reloadData];

     tableView1.delegate=self;

     tableView1.dataSource=self;
}

-(void)searchImagesInCategory:(NSString *)string
{
    string1=string;

    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory= [paths objectAtIndex:0];

    NSString *path=[documentsDirectory stringByAppendingPathComponent:@"1.sqlite"];


    //Open the database
    //might have to make database as property
    if(sqlite3_open([path UTF8String], &dataBase) ==SQLITE_OK)
    {
        sqlite3_stmt *statement;
        NSString *strSQL = [[NSString alloc]init];

        strSQL = @"select ImageName from tblLanguageElement where Category='";

        strSQL = [[strSQL stringByAppendingString:string1] stringByAppendingString:@"'"];

        const char *bar = [strSQL UTF8String]; 
        if(sqlite3_prepare(dataBase, bar, -1, &statement, NULL) == SQLITE_OK)
        {
            while (sqlite3_step(statement) == SQLITE_ROW) 
            {
                NSLog(@"%@",[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]);
                NSString *string2=[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
              // [imageName addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]];
                NSLog(string2);
                [imageName addObject:string2];
                [imageName retain];
            }

            //tableView1.delegate=self;
            //tableView1.dataSource=self;
            [self.tableView1 reloadData];
        }
    }
    //return 1;
    //[tableView1 reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    static NSString *CellIdentifier = @"Cell";



    UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];


    }

    cell.imageView.image= [UIImage imageNamed:[imageName objectAtIndex:indexPath.row]];

    return cell;
}

这是我的代码。为什么我的数组(imageName)没有获取方法内的数据以及为什么表视图没有重新加载?

In my app I am using an array to get some data inside the method but every time after adding the object to the array, I check its contents then always it shows me 0 objects inside the array and also after that I am reloading the table view but nothing is happening.no event are called. I am showing my code here:

in .h file

@interface ModalView:UIViewController

           <UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate> 

{

NSMutableArray *imageName;

}


@property (nonatomic,retain)NSMutableArray *imageName;

in .m file:-

@synthesize imageName;

      - (void)viewDidLoad {

       [super viewDidLoad];

     imageName=[[NSMutableArray alloc] init];

        [tableView1 reloadData];

     tableView1.delegate=self;

     tableView1.dataSource=self;
}

-(void)searchImagesInCategory:(NSString *)string
{
    string1=string;

    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory= [paths objectAtIndex:0];

    NSString *path=[documentsDirectory stringByAppendingPathComponent:@"1.sqlite"];


    //Open the database
    //might have to make database as property
    if(sqlite3_open([path UTF8String], &dataBase) ==SQLITE_OK)
    {
        sqlite3_stmt *statement;
        NSString *strSQL = [[NSString alloc]init];

        strSQL = @"select ImageName from tblLanguageElement where Category='";

        strSQL = [[strSQL stringByAppendingString:string1] stringByAppendingString:@"'"];

        const char *bar = [strSQL UTF8String]; 
        if(sqlite3_prepare(dataBase, bar, -1, &statement, NULL) == SQLITE_OK)
        {
            while (sqlite3_step(statement) == SQLITE_ROW) 
            {
                NSLog(@"%@",[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]);
                NSString *string2=[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
              // [imageName addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]];
                NSLog(string2);
                [imageName addObject:string2];
                [imageName retain];
            }

            //tableView1.delegate=self;
            //tableView1.dataSource=self;
            [self.tableView1 reloadData];
        }
    }
    //return 1;
    //[tableView1 reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    static NSString *CellIdentifier = @"Cell";



    UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];


    }

    cell.imageView.image= [UIImage imageNamed:[imageName objectAtIndex:indexPath.row]];

    return cell;
}

This is my code. Why my array (imageName) is not getting the data inside the method and why table view is not reloading?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半衾梦 2024-11-23 20:47:23

我知道您正在 viewDidLoad 中实例化数组,但是如果您将其添加到方法 -(void)searchImagesInCategory:(NSString *)string 的开头会发生什么:

imageName = [[NSMutableArray alloc] init];

I know you're instantiating your array in viewDidLoad, but what happens if you add it at the beginning of the method -(void)searchImagesInCategory:(NSString *)string:

imageName = [[NSMutableArray alloc] init];
天邊彩虹 2024-11-23 20:47:23

由于您在头文件中将对象声明为非原子保留,然后在实现中合成并分配它,因此每次向其中添加内容时都不需要保留它。您应该尝试删除 [imageName keep];

Since you declared the object in your header file as nonatomic retain, then synthesized and allocated it in your implementation, you do not need to retain it each time you add something to it. You should try removing the [imageName retain];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文