2011-12-18 152 views
1

我有以下属性和方法的类:下面迭代通过对象的一个​​NSMutableArray

头文件 - 注意,我并没有复制/粘贴的所有代码(仅pertinant信息):

@interface SQLiteDB : NSObject 

@property (nonatomic, strong) NSMutableArray *allAccountsArray; 
@property (nonatomic, strong) NSString *accountId, *accountName, *accountDescription, *accountTags, *accountPhoto, *accountCreationDate; 

+(id) populateAccountObjectWithId:(NSString *)id andName:(NSString *)name andDescription:(NSString *)description andTags:(NSString *)tags andPhoto:(NSString *)photo andCreationDate:(NSString *)creationDate; 

@end 

下面执行文件 - 注意,我并没有复制/粘贴的所有代码(仅pertinant信息):

+(id) populateAccountObjectWithId:(NSString *)id andName:(NSString *)name andDescription:(NSString *)description andTags:(NSString *)tags andPhoto:(NSString *)photo andCreationDate:(NSString *)creationDate 
{ 
    SQLiteDB *mySQLiteDB = [[self alloc] init]; 
    mySQLiteDB.accountId = id; 
    mySQLiteDB.accountName = name; 
    mySQLiteDB.accountDescription = description; 
    mySQLiteDB.accountTags = tags; 
    mySQLiteDB.accountPhoto = photo; 
    mySQLiteDB.accountCreationDate = creationDate; 
    return mySQLiteDB; 
} 

然后,在实现文件的另一种方法获取来自SQLite的DAT所有帐户ABASE:

-(id) fetchAccountList 
{ 
    // do some database stuff here 
    // create prepared statement, open database, etc... 

    allAccountsArray = [[NSMutableArray alloc] init]; 

    while(sqlite3_step(statement) == SQLITE_ROW) 
    { 
     NSString *thisAccountId =    [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement,0)]; 
     NSString *thisAccountName =   [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)]; 
     NSString *thisAccountDescription =  [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)]; 
     NSString *thisAccountTags =   [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)]; 
     NSString *thisAccountPhoto =   [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 4)]; 
     NSString *thisAccountCreationDate = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 5)]; 

     [allAccountsArray addObject:[SQLiteDB populateAccountObjectWithId:thisAccountId andName:thisAccountName andDescription:thisAccountDescription andTags:thisAccountTags andPhoto:thisAccountPhoto andCreationDate:thisAccountCreationDate]]; 

    } 
    // error handling code, etc. 
    // finalize, & close code here... 

return allAccountsArray; 

}

现在总算问题。在其他类中,我想用这个返回的对象数组来做东西。比如我会在TableVeiw控制器做到这一点:

-(void)loadView 
{ 
    [super loadView]; 

    mySQLiteDB = [[SQLiteDB alloc] init]; 
    allAccountsArray = [mySQLiteDB fetchAccountList]; 
} 

我会用这个晚些时候例如填充cellForRowAtIndexPath方法中的表列表。也许表格的每个单元格都会包含accountName,accountDescription和accountCreationDate。我不知道但是如何从对象的数组中访问该名称,降序,日期...

这显然会产生一个错误:

cell.textLabel.text = [allAccountsArray objectAtIndex:indexPath.row]; 

因为“行”的对象是“对象“包含名称,desc,日期等...

所以Stackoverflow,我问你...如何完成获取对象变量在数组的每个元素?

回答

2

你应该能够做到这样简单的事情,因为这:

SqliteDB *mySqliteDB = (SQliteDB *)[allAccountsArray objectAtIndex:indexPath.row]; 
NSString *myText = mySqliteDB.thisAccountID; 
myText = [myText stringByAppendingString:mySqliteDB.thisAccountName]; 
.... etc. 
cell.textLabel.text = myText; 
+0

这工作完美,非常感谢你! – ElasticThoughts

1

我觉得enumerateObjects:usingBlock:是你想要的迭代,即枚举,对象。你可能错过了它,因为它在超类中。

+0

嗡嗡声,好的我从来没有用过enumerateObjects:usingBlock:但是会查看它!感谢发布!仅供参考 - 我需要约4周的时间来开发任何使用Obj C的东西。 – ElasticThoughts