【问题标题】:IOS: searchbar in tableviewIOS:tableview中的搜索栏
【发布时间】:2011-09-04 16:03:00
【问题描述】:

我在 tableview 中创建了一个搜索栏,但是我在使用这个方法委托时遇到了问题,因为我用另一个数组中的数组填充了 table view...我显示了我的代码:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
ProgramAppDelegate *appDelegate = (ProgramAppDelegate *)[[UIApplication sharedApplication] delegate];
[tableData removeAllObjects];// remove all data that belongs to previous search
if([searchText isEqualToString:@""] || searchText==nil){
    [myTableView reloadData];
    return;
}
NSInteger counter = 0;
for(NSString *name in appDelegate.globalArray )
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    NSRange r = [name rangeOfString:searchText];
    if(r.location != NSNotFound)
    {
        if(r.location== 0)//that is we are checking only the start of the names.
        {
            [tableData addObject:name];
        }
    }
    counter++;
    [pool release];
}
[myTableView reloadData];
}

你可以看到代码“for(NSString *name in appDelegate.globalArray)”它不起作用,因为我用这个全局数组中的数组元素填充表格视图,我举个例子

在我的表格视图的一行中有一个 uitableviewcell,里面有四个标签; 我用这个 globalArray 的字符串写了这些标签,但是这样:

[cell.label1 setText:[[appDelegate.globalArray objectAtIndex:indexPath.row]objectAtIndex:1]];
[cell.label2 setText:[[appDelegate.globalArray objectAtIndex:indexPath.row]objectAtIndex:2]];
[cell.label3 setText:[[appDelegate.globalArray objectAtIndex:indexPath.row]objectAtIndex:3]];
[cell.label4 setText:[[appDelegate.globalArray objectAtIndex:indexPath.row]objectAtIndex:4]];

然后在搜索栏的委托方法中,代码“for(NSString *name in appDelegate.globalArray)”不起作用,我该如何更改我的代码?

** 我并不是说我只想检查 LABEL1 以进行搜索

【问题讨论】:

    标签: xcode ios uitableview uisearchbar


    【解决方案1】:

    这里的问题是globalArray 是一个数组数组。所以循环应该是这样的。

    for(NSArray *rowArray in appDelegate.globalArray )
    {
        for ( NSString *name in rowArray ) {
            // Do your processing here..
        }
    }
    

    使用tableData

    在您的tableView:cellForRowAtIndexPath: 中,执行此操作

    [cell.label1 setText:[[tableData objectAtIndex:indexPath.row]objectAtIndex:1]];
    [cell.label2 setText:[[tableData objectAtIndex:indexPath.row]objectAtIndex:2]];
    [cell.label3 setText:[[tableData objectAtIndex:indexPath.row]objectAtIndex:3]];
    [cell.label4 setText:[[tableData objectAtIndex:indexPath.row]objectAtIndex:4]];
    

    在你的numberOfSectionsInTableView:return 1

    在你的tableView:numberOfRowsInSection:return [tableData count];

    您应该包含一个在用户停止搜索时将搜索数据重置为原始内容的方法。

    - (void)resetSearchData {
        // Get appDelegate first.
    
        self.tableData = [NSArray arrayWithArray:appDelegate.globalArray];
    }
    

    【讨论】:

    • 现在没问题,但不搜索,当我在搜索栏中写名字时,表格视图中的元素保持不变
    • 这里有两件事 - [tableData addObject:name]; 应该是 [tableData addObject:rowArray]; 并且您应该在 tableView:cellForRowAtIndexPath: 方法中将 appDelegate.globalArray 替换为 tableData
    • 第一件事没问题;我在 loadview 中做的第二件事:[tableData addObjectsFromArray:appDelegate.globalArray];
    • loadView 仅在控制器将其视图属性设置为 nil 时调用一次。所以它对过滤结果的作用不大。当您将搜索结果保存在 tableData 时,我不明白您为什么要从 appDelegate.globalArray 加载数据。
    • 那我该怎么办?你应该怎么做?
    猜你喜欢
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多