如何在uitableview中添加搜索栏?

发布于 2024-10-29 11:46:20 字数 229 浏览 4 评论 0原文

我在 UITableView 中显示了一个 NSMutableArray,并且我在其中添加了一些元素。

例如,元素名称为 First、FirstTwo、Second、SecondTwo、Third、ThirdTwo。

现在我想在屏幕中添加一个搜索栏。在该搜索栏中,当我键入 F 时,表格应仅显示 First 和 FirstTwo。

我该怎么做?

I have an NSMutableArray displayed in a UITableView, and I have added some elements there.

For example, element names are First, FirstTwo, Second, SecondTwo, Third, ThirdTwo.

Now I want to add a search bar in the screen. In that search bar when I type F, the table should only show First and FirstTwo.

How should I do this?

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

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

发布评论

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

评论(4

生生不灭 2024-11-05 11:46:20
 searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

 searchBar.delegate = self;

 self.tableView.tableHeaderView = searchBar;
 searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

 searchBar.delegate = self;

 self.tableView.tableHeaderView = searchBar;
挖个坑埋了你 2024-11-05 11:46:20

掌握窍门的最佳方法是按照此处的教程 这里
您正在寻找的部分是这样的:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
   [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 dataSource)
   {
      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];
}

The best way to get the hang of this is by following a tutorial over here over here.
The part you are looking for, is this:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
   [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 dataSource)
   {
      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];
}
小清晰的声音 2024-11-05 11:46:20

如果您使用搜索栏。然后您可以使用搜索栏的委托方法搜索表格视图的内容

(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
// myArray is main array for Table View
// tempMainArray which store myArray Data

myArray = [[NSMutableArray alloc]initWithArray:tempMainArray];
NSMutableArray *searchArray = [[NSMutableArray alloc]init];


[searchArray removeAllObjects];// remove all data that belongs to previous search
if([searchText isEqualToString:@""]|| searchText==nil){

    [myArray removeAllObjects];
    myArray =  tempMainArray;
    [_objTableView reloadData];

    return;
}

NSInteger counter = 0;
for(NSString *name in myArray)
{

    NSRange r = [name rangeOfString:searchText];
    if(r.location != NSNotFound)
    {
        if(r.location== 0)//that is we are checking only the start of the names dynamicaly.
        {
            [searchArray addObject:name];
        }
    }

    counter++;


}

if (searchArray.count > 0) {

    [myArray removeAllObjects];
     myArray =  searchArray;
    [_objTableView reloadData];
}
else
{
    [myArray removeAllObjects];
    myArray =  tempMainArray;
    [_objTableView reloadData];
}

}

If you are using search bar. then You can search your contain of table view using delegate method of searchbar

(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
// myArray is main array for Table View
// tempMainArray which store myArray Data

myArray = [[NSMutableArray alloc]initWithArray:tempMainArray];
NSMutableArray *searchArray = [[NSMutableArray alloc]init];


[searchArray removeAllObjects];// remove all data that belongs to previous search
if([searchText isEqualToString:@""]|| searchText==nil){

    [myArray removeAllObjects];
    myArray =  tempMainArray;
    [_objTableView reloadData];

    return;
}

NSInteger counter = 0;
for(NSString *name in myArray)
{

    NSRange r = [name rangeOfString:searchText];
    if(r.location != NSNotFound)
    {
        if(r.location== 0)//that is we are checking only the start of the names dynamicaly.
        {
            [searchArray addObject:name];
        }
    }

    counter++;


}

if (searchArray.count > 0) {

    [myArray removeAllObjects];
     myArray =  searchArray;
    [_objTableView reloadData];
}
else
{
    [myArray removeAllObjects];
    myArray =  tempMainArray;
    [_objTableView reloadData];
}

}
痴梦一场 2024-11-05 11:46:20

我自己做了,效果非常好。

声明两个可变数组。第一个包含所有数据,第二个用于存储过滤后的数据。这里的searchuser是搜索栏出口。 aray 是存储所有数据的主数组。将搜索栏大写属性设置为句子,以便它可以正常工作。

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [searchuser.text lowercaseString]);
    filteredarray=[[NSMutableArray alloc]init];
    int l=(int)[searchuser.text length];
    if (l>0)
    {
        NSMutableString * data=[[NSMutableString alloc]init];
        int a=(int)[aray count];

        for (int i=0;i<=a-1;i++)
        {
            data=[aray objectAtIndex:i];
            if ([searchuser.text isEqualToString:data])
            {
                [filteredarray addObject:data];
            }
            if ([data length]>l)
            {
                NSMutableString * string=[[NSMutableString alloc]initWithString:[data substringWithRange:NSMakeRange(0, l)]];

                if ([searchuser.text isEqualToString:string])
                {
                    [filteredarray addObject:data];

                }
            }
        }
    }
    else
    {
        filteredarray=aray;
    }
    [tableview reloadData];
}

I did it myself and it is working absolutely fine.

Declare two mutable arrays. One containing all the data and second for storing filtered data. Here searchuser is search bar outlet. aray is main array storing all data. set searchbar capitalisation property to sentence so that this would work correctly.

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [searchuser.text lowercaseString]);
    filteredarray=[[NSMutableArray alloc]init];
    int l=(int)[searchuser.text length];
    if (l>0)
    {
        NSMutableString * data=[[NSMutableString alloc]init];
        int a=(int)[aray count];

        for (int i=0;i<=a-1;i++)
        {
            data=[aray objectAtIndex:i];
            if ([searchuser.text isEqualToString:data])
            {
                [filteredarray addObject:data];
            }
            if ([data length]>l)
            {
                NSMutableString * string=[[NSMutableString alloc]initWithString:[data substringWithRange:NSMakeRange(0, l)]];

                if ([searchuser.text isEqualToString:string])
                {
                    [filteredarray addObject:data];

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