UISegmentedControl not updating view

流过昼夜 提交于 2019-12-05 21:14:13

reloadData will reload the tableView, and when you reload the tableView all headerViews will be reloaded too. When the tableView was reloaded there is a new header. And the UISegmentedControl you see now is not the one that you have tapped.

Create an ivar that holds your selected index

@implementation .. {
    NSInteger selectedIndex;
}

when creating the view restore the saved index

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    /* ... */
    control = [[UISegmentedControl alloc] initWithItems:itemArray];
    control.selectedSegmentIndex = selectedIndex;
    /* ... */
}

save the index when changing the segmentedControl

- (void)changeFilter:(id)sender {
    UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
    selectedIndex = segmentedControl.selectedSegmentIndex;
    if (segmentedControl.selectedSegmentIndex == 0) {
        selectedFilterInfo = @"Alle";
        filteredArray = nil;
    }
    /* ... */

    // reloads the table and all header views!
    [tableView reloadData];
}

And btw. when you use UISegmentedControls there is no need to call setSelectedSegmentIndex: on it again, the tap sets the index already. And reloadInputViews is totally useless for a UISegmentedControl. But I guess this code was just added because it didn't work. Don't forget to remove it ;-)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!