Filtering UITableViewCells with animation - iPhone Development

后端 未结 2 1589
抹茶落季
抹茶落季 2021-01-27 09:51

This seems simple enough but as yet I am unable to find a solution.

Basically I have a segmented control with two options. The first is the default (and is automatically

相关标签:
2条回答
  • 2021-01-27 10:01

    Have you looked into reloadSections:withRowAnimation:?

    The basic idea is to call reloadSections:withRowAnimation: and in your UITableViewDataSource implementation switch on the segmented control's selectedSegmentIndex.

    Assuming your data is flat (only one section) it would look something like this:

    - (IBAction)segmentSwitch:(id)sender
    {
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        switch (self.segmentedControl.selectedSegmentIndex)
        {
            default:
            case 0:
                return [self.allRows count];
            case 1:
                return [self.onlySomeRows count];
        }
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        id data;
        switch (self.segmentedControl.selectedSegmentIndex)
        {
            default:
            case 0:
                data = [self.allRows objectAtIndex:[indexPath row]];
                break;
            case 1:
                data = [self.onlySomeRows objectAtIndex:[indexPath row]];
                break;
        }
    
        //TODO: use data to populate and return a UITableViewCell...
    }
    
    0 讨论(0)
  • 2021-01-27 10:24

    You can accomplish a similar effect by calling deleteRowsAtIndexPaths:withRowAnimation: and insertRowsAtIndexPaths:withRowAnimation: on your table view with a UITableViewRowAnimationFade animation.

    0 讨论(0)
提交回复
热议问题