数组未填充新数据
在我的应用程序中,我使用数组来获取方法内的一些数据,但每次将对象添加到数组后,我都会检查其内容,然后总是显示数组内的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道您正在 viewDidLoad 中实例化数组,但是如果您将其添加到方法
-(void)searchImagesInCategory:(NSString *)string
的开头会发生什么: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 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];