开发者

Unable to load data into the table view

开发者 https://www.devze.com 2023-03-14 14:36 出处:网络
In my app.i have some classes and inside one class I am calling method in other class. In my other class I have a table view and an array \"data\". When the query is executed in this method then I am

In my app.i have some classes and inside one class I am calling method in other class. In my other class I have a table view and an array "data". When the query is executed in this method then I am adding the data obtained from the execution of the query to the array and then I am calling reload data for the table view. But to my surprise table view is 开发者_运维百科not getting reloaded and I am not getting any sort of data on the table view. It is showing just a blank table view.

The method which is called from other class:

-(void)searchImagesInCategory:(NSString *)string
{
    data=[[NSMutableArray alloc]init];
    string1=string;    
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory= [paths objectAtIndex:0];
    NSString *path=[documentsDirectory stringByAppendingPathComponent:@"Table.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]; 
        //strSQL = [strSQL stringByAppendingString:"''"];
        //NSLog(strSQL);
        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)]);
                [data addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]];
            }

            tableView1.delegate=self;
            tableView1.dataSource=self;
        }

    }
    [tableView1 reloadData];
}

My cell for row at index path method:-

// Customize the appearance of table view cells.
- (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:[data objectAtIndex:indexPath.row]];
    return cell;        
}

Why i am not getting the reloaded Table view.Please help.

Thanks,

Christy


In your method you are re-initializing your "data" array. Try making this a local variable by declaring it in your header file and then in your viewDidLoad method call:

     data= [[NSMutableArray alloc]init];

Also there is no need to reset the delegate and data source. Just do it once and leave it.

This is an edit from something else I have seen. You need to bind your variable and not just append the string. Here is an example....

- (NSMutableArray*) getAllImagesForCategory:(NSString*)category {
NSMutableArray *tempArray = [[NSMutableArray alloc]init]autorelease];;

   const char *sqlStatement = "select ImageName from tblLanguageElement where Category=?";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {

    sqlite3_bind_text(compiledStatement,1,category); //this is where you put your variable into the statement

    while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
        // Read the data from the result row

        NSString *imageName = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 1)];


        [tempArray addObject:imageName];

    }
} else {
    NSLog(@"ERROR: Failed to select all images with message '%s'",sqlite3_errmsg(database));
}

   // Release the compiled statement from memory
   sqlite3_finalize(compiledStatement);
   return tempArray;
}

So this is the SQL statement that will get the stuff from the database. To call this method you go something like this.

_data = [self getAllImagesForCategory];
[tableView1 reloadData];

I hope this helps any questions just ask.


Make sure you are returning the correct number in tableView:numberOfRowsInSection:.

0

精彩评论

暂无评论...
验证码 换一张
取 消