Question

I have one TableView. It recieve data from network by using JSON and fill the data into cells. Also, I have SegmentedControl above TableView. I have to understand how can I show different data (by 'type' parameter) by clicking each segment of segmentedControl.

Everything work well (my code below). But I'd like to split data to each segment by use 'type' parameter in data array

My JSON code:

[{
    "title":"message one",
    "subtitle":"sub one",
    "type":1
},{
    "title":"message two",
    "subtitle":"sub two",
    "type":2
}, {
    "title":"message three",
    "subtitle":"sub three",
    "type":2
}, {
    "title":"message four",
    "subtitle":"sub four",
    "type":3
}]

SegmentedControl:

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"All", @"Present", @"Missing", nil]];
    [segmentedControl addTarget:self action:@selector(segmentedControlChanged:) forControlEvents:(UIControlEventValueChanged)];
    segmentedControl.frame = CGRectMake(0, 0, 100, 30);
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    segmentedControl.selectedSegmentIndex = 0;
    [self.tableView addSubview:segmentedControl];

segmentedControlChanged method

- (void) segmentedControlChanged:(id)sender
    {
        UISegmentedControl* segmentedControl = (UISegmentedControl*)sender;
        segmentIndex = segmentedControl.selectedSegmentIndex; // segmentIndex defined in .h file
        [self.tableView reloadData];
    }

Let's show some cells

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    UILabel *titleField = (UILabel *)[cell viewWithTag:10];
    UILabel *subField = (UILabel *)[cell viewWithTag:11];

    titleField.text = [NSString stringWithFormat:@"%@", [[dataList objectAtIndex:indexPath.row] valueForKey:@"title"]];
    subField.text = [NSString stringWithFormat:@"%@", [[dataList objectAtIndex:indexPath.row] valueForKey:@"subtitle"]];

    switch (segmentIndex)
    {
        case 0:
            cell.backgroundColor = [UIColor brownColor];
            break;
        case 1:
            cell.backgroundColor = [UIColor whiteColor];
            break;
        case 2:
            cell.backgroundColor = [UIColor blueColor];
            break;
        default:
            break;
    }

    return cell;
}

Thank you and sorry for my english!

Was it helpful?

Solution

Create a second property which holds the filtered objects for the currently selected tab, and then set all of your table view methods to fetch from that array instead.

Declare the property:

@property (strong, nonatomic) NSArray *filteredObjects;

In your segmentControlChanged: method:

self.filteredObjects = [self.dataList filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"type == %d", segmentedControl.selectedSegmentIndex]];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top