如何指定过滤现有UITableView的条件?

时间:2013-05-22 01:07:14

标签: objective-c uitableview

快速提问。我在一个视图控制器中放置了一个按钮,在另一个视图控制器中放置了一个UITableView(它已经填充了包含标题,描述等的自定义单元格)。我希望我的用户能够按下按钮,并使UITableView显示过滤结果。

代码会是什么样的?如何使用此按钮按指定条件过滤我的UITableView?

以下是按钮的viewcontroller.m文件中的代码:

- (IBAction)buttonpressed:(UIButton *)sender {

        NSLog(@"Button Pushed!");



}

以下是我的TableViewController.m文件的样子:

- (int)numberOfSectionsInTableView: (UITableView *)tableview

{

    return 1;

}

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [searchResults count];




    } else {
        return [Strains count];

    }



}


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

    StrainTableCell *cell = (StrainTableCell *)[tableView dequeueReusableCellWithIdentifier:strainTableIdentifier];
    if (cell == nil) 


        cell = [[StrainTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strainTableIdentifier];



        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StrainTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

    if (tableView == self.searchDisplayController.searchResultsTableView) {


        cell.titleLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Title"];
        cell.descriptionLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Description"];
        cell.ratingLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Rating"];


        NSLog(@"%@", searchResults);
    } else {
        cell.titleLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Title"];
         cell.descriptionLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Description"];
         cell.ratingLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Rating"];



    }


    {

    } 


return cell;

}

1 个答案:

答案 0 :(得分:0)

当您使用按钮方法创建表视图控制器时(如果您正在使用故事板,则将按钮连接到第二个视图控制器,然后在prepareForSegue中执行此操作,否则只需在按钮内创建视图控制器的实例按下并将其推送到导航控制器。)

向表视图控制器添加属性或实例变量,指定要应用的过滤器,并在创建表视图控制器时进行设置,然后在viewDidLoad和/或cellForRowAtIndexPath中进行过滤

相关问题