【问题标题】:Search bar for table view表格视图的搜索栏
【发布时间】:2015-04-16 08:39:54
【问题描述】:

我想在一个视图控制器中有 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

【问题讨论】:

    标签: ios objective-c uitableview uisearchbar


    【解决方案1】:

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

    然后你可以只对你想要的那个执行操作。

    示例:

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

    【讨论】:

    • 我这样做了,但是当我想搜索某些东西时,会调用 cellForRowAtIndexPath 并且将完成搜索的表是 nonSearchableTableView
    • 能否发布您的搜索方式。你自己实现了吗?因为我试过这个,我没有任何问题。我通过代码在 _searchableTableView 上调用 cellForRowAtIndexPath。
    【解决方案2】:

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

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

    然后实现table view的委托方法

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

    同样的方式实现delegate的cellForRowAtIndexPath方法。

    【讨论】:

    • 为表视图创建属性并使用属性名称可以区分表。像上面的代码 --- if([tableView isEqual:table1])
    • 尝试使用断点进行调试。我认为你在这里遗漏了一些东西
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多