繁体   English   中英

搜索栏表格视图

[英]Search bar for table view

我希望在一个视图控制器中有2个表,并且只在其中一个中进行搜索。 我能怎么做 ?

我知道如何实现搜索栏,但我不知道如何在表之间做出改变。 我试图用标签做这个,但是没有用。

任何建议都会有所帮助。

UITableViewCell *cell ;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";


    if (tableView.tag==1) {

   cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        cell.textLabel.text = [self.searchResult objectAtIndex:indexPath.row];

    }
    else
    {
        cell.textLabel.text = self.tableData[indexPath.row];
    }}
    else if (tableView.tag==0) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];

        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        cell.textLabel.text = [historique objectAtIndex:indexPath.row];

    }

    return cell;

}

我也在尝试这个:

即使我这样说

self.tableSource= self.searchDisplayController.searchResultsTableView; the result is the same

使用变量来处理这两个表。

然后,您只能对所需的操作执行操作。

例如:

@interface MyClass
@property (nonatomic, strong) UITableView *searchableTableView;
@property (nonatomic, strong) UITableView *nonSearchableTableView;
@end

您需要为两个表视图创建两个属性。

@property (strong, nonatomic) IBOutlet UITableView *table1;
@property (strong, nonatomic) IBOutlet UITableView *table2;

然后实现表视图的委托方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
     if([tableView isEqual:table1])
     {
        return YOUR_VALUE;
     }
     else
     {
        return YOUR_VALUE;
     }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if([tableView isEqual:table1])
{
    return [array1 count]
}
else
{
        return [array2 count];
}

以同样的方式实现委托的cellForRowAtIndexPath方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM