过滤Tableview

时间:2015-10-23 18:50:53

标签: ios objective-c uitableview

如何根据选择器值过滤tableview? 我有以下NSMutableArray:

[0]:对象1(name1,十月)

[1]:对象2(name2,November)

[2]:对象3(name3,March)

[3]:对象4(name4,April)

[4]:对象5(name5,April)

表视图显示这些对象的值。 我有一个月值的选择器(1月,2月,等等) 因此,根据用户选择的选择器值,表格视图应显示值,例如,如果我选择4月份,则只显示对象3和4这些值。

解决方案代码,感谢Fennelouski :(只是想我把它放在这里以防有人需要它)

    NSMutableArray *array;
    NSMutableArray *filteredArray;
    int monthInt;
    NSString *monthString;

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


    Titles *object1 = [[Titles alloc]init];
    [object1 setTitle:@"Title1"];
    [object1 setMonth:@"10"];

    Titles *object2 = [[Titles alloc]init];
    object2.title = @"Title2";
    object2.month = @"10";
    Titles *object3 = [[Titles alloc]init];
    object3.title = @"Title3";
    object3.month = @"4";
    array = [[NSMutableArray alloc]init];
    filteredArray = [[NSMutableArray alloc]init];
    [array addObject:object1];
    [array addObject:object2];
    [array addObject:object3];

    pickerData = @[@"Jan", @"Feb", @"March", @"April", @"May", @"June", @"July", @"Aug", @"Sept", @"October", @"Nov", @"Dec"];

    NSDate *currentDate = [NSDate date];
    NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:currentDate];
    monthInt= [components month];
    monthString = [NSString stringWithFormat:@"%li",(long)monthInt];
    for (Titles *item in array)
    {
        if([item.month isEqualToString:monthString])
        {
            [filteredArray addObject:item];
        }
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [filteredArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    Titles *titles = filteredArray[indexPath.row];
    cell.textLabel.text = titles.title;
    cell.imageView.image = [UIImage imageNamed:@"totoro.jpg"];
    NSLog(@"Cell is %@", [array objectAtIndex:indexPath.row]);
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// The number of rows of data
- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return pickerData.count;
}

// The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return pickerData[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

    NSLog(@"Row: %li", (long)row);

    monthString = [NSString stringWithFormat:@"%li",row+1];
    [filteredArray removeAllObjects];
    for (Titles *item in array)
    {
        if([item.month isEqualToString:monthString])
        {
            [filteredArray addObject:item];
        }
    }
    [self.tableview reloadData];
}

1 个答案:

答案 0 :(得分:1)

您似乎遵循了将表视图表示存储在单独数据结构中的信息的设计模式。这很好,使过滤更容易。

幸运的是,您不必在表视图中做太多事情来过滤结果。您需要做的就是过滤表视图正在读取的数据结构,然后更新表视图。

我建议在可变数组和表视图之间添加一个数组。

某些喜欢,这应该适合你

NSMutableArray *filteredObjects = [NSMutableArray new];
for (MYObjectClass *object in myMutableArray) {
    if ([object.stringProperty isEqualToString:@"Filter"]) {
        [filteredObjects addObject:object];
    }
}

然后让您的表格视图参考filteredObjects而不是您使用的包含所有数据的数组。

相关问题