我想用NSMutableArray的2列拉结果。 - “名称”列 - >用户在用户开始输入内容时显示在tableview中 - “地址”colomn - >将用于发送到另一个视图。
以下是我的工作:
@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循环,但现在我不知道。
答案 0 :(得分:0)
您可以使用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];