Objective c 如何在NSMUtableArray中发送多个列以及如何实现它
我想使用NSMutableArray提取具有2列的结果。 -“名称”列-->用于在用户开始键入内容时在tableview中显示 -“地址”列-->将用于发送到另一个视图 以下是我的工作:Objective c 如何在NSMUtableArray中发送多个列以及如何实现它,objective-c,xcode6,Objective C,Xcode6,我想使用NSMutableArray提取具有2列的结果。 -“名称”列-->用于在用户开始键入内容时在tableview中显示 -“地址”列-->将用于发送到另一个视图 以下是我的工作: @interface ViewController () { ViewController *SampleViewController; NSMutableArray *totalStrings; NSMut
@interface ViewController ()
{
ViewController *SampleViewController;
NSMutableArray *totalStrings;
NSMutableArray *filteredStrings;
BOOL isFiltered;
}
@implementation ViewController
- (void)viewDidLoad {
[self createOrOpenDB]; ////Call database
NSMutableArray* result=[[NSMutableArray alloc] init];
NSString *ask = [NSString stringWithFormat:@"SELECT * FROM tb_001"];
sqlite3_stmt *statement;
const char *query_statement = [ask UTF8String];
const char *dbpath = [_DBPath UTF8String];
if (sqlite3_open(dbpath, &_iDB) == SQLITE_OK){
{
if (sqlite3_prepare_v2(_iDB, query_statement, -1, &statement, nil) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *nameField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 1)];
NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 2)];
//add your namefield here.
[result addObject:xxxxxxxxxxxxxxx];
}
sqlite3_finalize(statement);
sqlite3_close(_iDB);
}
}
totalStrings=[[NSMutableArray alloc] initWithArray:result];
}
Here is where I will use the first column
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (searchText.length ==0){
isFiltered = NO;
}
else
{
isFiltered = YES;
filteredStrings =[[NSMutableArray alloc]init];
for (NSString *str in totalStrings) {
NSRange StringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (StringRange.location !=NSNotFound) {
[filteredStrings addObject:str];
}
}
}
[self.myTableView reloadData];
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self.myTableView resignFirstResponder];
}
//table view datasource and delegate methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (isFiltered){
return [filteredStrings count];
}
return [totalStrings count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier= @"Cell";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if(!isFiltered){
cell.textLabel.text =[totalStrings objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else{ //It is filtered
cell.textLabel.text = [filteredStrings objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//here I want to use result from another column (address)
LadyGagaVC *dada =[self.storyboard
[dada setText:[resilt_from_anothercolomn objectAtIndex:indexPath.row]];
}
如何将其放入正确的数组并使用它?
我知道我必须使用for循环,但现在我不知道。您可以使用
NSDictionary
,如下所示:
NSString *nameField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 1)];
NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 2)];
NSDictionary *data = @{@"name":nameField,
@"address":addressField};
//add your namefield here.
[result addObject:data];
然后:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//here I want to use result from another column (address)
LadyGagaVC *vc = ...
NSDictionary *dic = totalStrings[indexPath.row];
NSString *name = dic[@"name"];
NSString *address = dic[@"address"];
//Use this values as you want...
}
或者最好创建一个类(模型),比如MyModel,它将具有两个属性:
@interface MyModel : NSObject
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *address;
@end
使用MyModel
的实例代替字典。例如
MyModel *model = [MyModel new];
model.name = nameField;
model.address = addressField;
[result addObject:model];
问题是如何实现上面的第一个for循环?如果第一个for循环没有实现,那么tableview将不会显示。我通过添加两个结果并将其分隔来找到它。一个结果用于表视图,另一个结果用于另一个viewcontroller。它起作用了。但是看起来很复杂。但是对于班级模式,我不知道为什么它不起作用。那门课放在哪里。h文件?顺便说一句,我强烈建议您使用sqlite的第三方库,因为您的代码将更加干净易读。谢谢您的回答和推荐。我有一个问题:如何在“totalstrings”中使用model.name?那么,您应该使用不同的名称,例如totalObjects,而不是
totalstrings
。totalObject中的每个对象都是类MyModel
,因此MyModel*model=totalObject[someIndex];型号。名称代码>