iOS:表格视图中的搜索栏

发布于 2024-11-10 13:52:47 字数 1617 浏览 4 评论 0原文

我正在表视图中创建搜索栏,但我对这个方法委托有问题,因为我用另一个数组内的数组填充表视图...我显示我的代码:

- (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]];

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

** 我并不是说我只想检查 LABEL1 的搜索情况

I am creating a searchbar in a tableview but I have problems with this method delegate because I fill the table view with array inside another array...I show my code:

- (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];
}

You can see the code "for(NSString *name in appDelegate.globalArray )" it don't work because I fill the table view with elements of arrays inside this global array, I make an example

In a row of my table view there is a uitableviewcell and inside it there are four label;
I write these label with string of this globalArray but in this way:

[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]];

then in delegate method for searchbar the code "for(NSString *name in appDelegate.globalArray )" don't work, How can I change my code?

** I DON'T SAY THAT I WANT TO CHECK ONLY LABEL1 FOR THE SEARCH

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛装女皇 2024-11-17 13:52:47

这里的问题是 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];
}

The problem here is that globalArray is an array of arrays. So the loop should be something like.

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

Using tableData

In your tableView:cellForRowAtIndexPath:, do this

[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]];

In your numberOfSectionsInTableView:, return 1;

In your tableView:numberOfRowsInSection:, return [tableData count];

You should include a method that will reset the search data to the original content when the user stops searching.

- (void)resetSearchData {
    // Get appDelegate first.

    self.tableData = [NSArray arrayWithArray:appDelegate.globalArray];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文