希望你能帮助我,我的麻烦是以下一个:
大多数类似的问题都是由于数组可用的“区域”(不是全局但是本地),但在这种情况下不是(我不这么认为)。
我没有警告,也没有来自xCode的错误。
以下是我的代码的一部分:
.h文件 #import
@interface MasterViewController : UITableViewController
@property (nonatomic, assign) int nbUsers;
@property (nonatomic, assign) NSMutableArray *nameOfUsers;
@end
viewDidLoad方法
- (void)viewDidLoad
{
[super viewDidLoad];
// Number of users
int tempNb = 0;
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [docPaths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"UserDatabase.sqlite"];
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
[...]
// Name of users
[database open];
int i = 0;
FMResultSet *results2 = [database executeQuery:@"SELECT * FROM userList"];
while ([results2 next]) {
[_nameOfUsers insertObject:[results2 stringForColumn:@"name"] atIndex:i];
NSLog(@"%@", _nameOfUsers[i]);
i++;
//[_nameOfUsers addObject:(NSString *)[results2 stringForColumn:@"name"]];
}
[database close];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
}
我想要做的是通过以下方式在主视图中将这些名称显示在我的单元格中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myBasicCell"];
cell.textLabel.text = _nameOfUsers[indexPath.row];
NSLog(@"%D and the name is %@", indexPath.row, _nameOfUsers[indexPath.row]);
return cell;
}
但麻烦不在这里,因为通过使用NSLog,我发现我的SQLite数据库已经充分填充,获取的值也被验证,它是未正确填充的NSMutableArray(可能是我用来填充它的方法)并不好,但我遵循了许多描述相同方法的类似问题。)
提前感谢您的时间,
此致
答案 0 :(得分:1)
对于ARC项目: @interface MasterViewController:UITableViewController
@property (nonatomic, readWrite) int nbUsers;
@property (nonatomic, strong) NSMutableArray *nameOfUsers;
@end
非ARC:
@interface MasterViewController : UITableViewController
@property (nonatomic, readWrite) int nbUsers;
@property (nonatomic, retain) NSMutableArray *nameOfUsers;
@end
并在viewDidLoad中:
- (void)viewDidLoad
{
[super viewDidLoad];
_nameOfUsers = [[NSMutableArray alloc] init];
// Number of users
int tempNb = 0;
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [docPaths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"UserDatabase.sqlite"];
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
[...]
// Name of users
[database open];
int i = 0;
FMResultSet *results2 = [database executeQuery:@"SELECT * FROM userList"];
while ([results2 next]) {
[_nameOfUsers insertObject:[results2 stringForColumn:@"name"] atIndex:i];
NSLog(@"%@", _nameOfUsers[i]);
i++;
//[_nameOfUsers addObject:(NSString *)[results2 stringForColumn:@"name"]];
}
[database close];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
}
答案 1 :(得分:0)
@property(nonatomic,assign)NSMutableArray * nameOfUsers;它应该是retain或strong(如果你使用ARC),并且你永远不会初始化nameOfUsers。