我在執行段控制時遇到了一些問題。因爲我想這是一個固定的頭,所以當我滾動我總能看到它,我已經在UISegmentControl在放置在tableView標題中時表現不尋常
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
一切都很好,直到在這裏實現它,會出現分段控制。問題是點擊段的時候。儘管具有選擇器實現的功能被調用,該段控制具有正確selectedSegmentIndex,區段不突出顯示不同的是最初在viewForHeaderInSection
設置與
sortControl.selectedSegmentIndex = 0;
之一。此細分受衆羣突出顯示且未突出顯示(再次按下時)。另一個奇怪的是,當我按其他片段,在0的片段變得突出顯示。
下面是viewForHeaderInSection
的完整代碼:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIColor *tintColor = [UIColor colorWithRed:241.0/255 green:78.0/255 blue:35.0/255 alpha:1];
sortControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:@"Distance", @"Rating", @"Name", nil]];
sortControl.segmentedControlStyle = UISegmentedControlStyleBar;
sortControl.tintColor = tintColor;
sortControl.frame = CGRectMake(20, 20, 280, 35);
sortControl.selectedSegmentIndex = 0;
[sortControl addTarget:self action:@selector(sortChanged) forControlEvents:UIControlEventValueChanged];
UIView *view=[UIView new];
view.frame = CGRectMake(0, 0, 320, 70);
view.backgroundColor =[UIColor blueColor];
[sortControl setEnabled:YES forSegmentAtIndex:0];
[sortControl setEnabled:YES forSegmentAtIndex:1];
[sortControl setEnabled:YES forSegmentAtIndex:2];
view.userInteractionEnabled = YES;
[view addSubview:sortControl];
return view;
}
感謝您的回覆,作爲AliSoftware,您一直非常樂於助人! – Alex
我之前也在評論中添加了相同的評論(即每次都重新創建了分段的片段),並建議使用ivar ......但使用GCD和displatch_once的好解決方案不會有這樣的效果:)還請注意,如果ARC沒有打開,那麼您在代碼中所說的泄漏只會出現。 – AliSoftware