问题
This may be a strange problem and I hope someone has had it before.
I added a SegmentControl to my TableView using this code:
UIView *headerView = [[UIView alloc] init ];
[headerView addSubview:resultsSegment];
self.tableView.tableHeaderView = headerView;
resultsSegment.frame = CGRectMake(45, 123, 250, 40);
[self.tableView addSubview:resultsSegment];
With help from these forums the first three lines made the Segment part of the TableView Header so that it stayed in place for scrolling. Great.
However, that disabled the ability to click on the SegmentControl.
Adding the last line made that possible again.
The SegmentControl performs great UNTIL scrolling, then it becomes really unresponsive. It doesn't throw up any errors and it does eventually accept a press from the finger, but you have to tap it 5/6 times before it switches.
If anyone can shed some light on this that would be amazing
Any extra information you need I'll be happy to provide!
EDIT ----
ViewController.h
@interface StdTCPTestViewController : UIViewController <UITableViewDataSource, UITableViewDelegate,UIScrollViewDelegate> {
NSTimer *Timer;
}
@property (nonatomic, strong) NSString *typeOfTest;
@property (nonatomic, strong) NSString *testLocation;
@property (nonatomic, strong) NSString *statusText;
@property (nonatomic, strong) NSString *showResultType;
@property (nonatomic, assign) NSInteger *progressInt;
@property (nonatomic, assign) NSString *testDirection;
@property (strong, nonatomic) IBOutlet UITextView *textView;
@property (strong, nonatomic) IBOutlet UIProgressView *testProgressBar;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, retain) NSArray *ResultTitles;
@property (nonatomic, retain) NSMutableArray *downloadResults;
@property (nonatomic, retain) NSMutableArray *uploadResults;
@property (strong, nonatomic) IBOutlet UISegmentedControl *resultsSegment;
- (IBAction)resultsSwitch:(id)sender;
Select areas of ViewController.m
- (void)viewDidLoad
{
[resultsSegment setTitle:@"Download" forSegmentAtIndex:0]; // Sets the title for the 1st segment button
[resultsSegment setTitle:@"Upload" forSegmentAtIndex:1]; // Sets the title for the 2nd segment button
[super viewDidLoad];
// UIView *headerView = [[UIView alloc] init ];
// [headerView addSubview:resultsSegment];
// self.tableView.tableHeaderView = headerView;
resultsSegment.frame = CGRectMake(45, 123, 250, 40);
[self.tableView addSubview:resultsSegment];
[self APISimpleDemo];
self.navigationItem.title = typeOfTest; // Set viewcontroller title to the type of test it is
}
- (IBAction)resultsSwitch:(id)sender {
if([sender selectedSegmentIndex] == 0){
showResultType = @"download";
[self.tableView reloadData];
}
else {
showResultType = @"upload";
[self.tableView reloadData];
}
}
#pragma mark Table Definitions
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // Default is 1 if not implemented
{
return 3;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
{
switch (section) {
case 0:
return @"";
break;
case 1:
return @"";
break;
case 2:
return @"";
break;
default:
return @"Section Count Error";
break;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
return 35;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
{
switch (section) {
case 0:
return 10;
break;
case 1:
return 22;
break;
case 2:
return 0;
break;
default:
return 22;
break;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
switch (section) {
case 0:
return 3;
break;
case 1:
return 0;
break;
case 2:
return [ResultTitles count];
break;
default:
return 0;
break;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *serverLoc = [tableView dequeueReusableCellWithIdentifier:@"speedCell"];
switch (indexPath.section) {
case 0:
switch (indexPath.row) {
case 0:
serverLoc.textLabel.text = @"Test location:";
serverLoc.detailTextLabel.text = testLocation;
break;
case 1:
serverLoc.textLabel.text = @"Status:";
serverLoc.detailTextLabel.text = statusText;
break;
case 2:
serverLoc.textLabel.text = @"Progress";
serverLoc.detailTextLabel.text = [NSString stringWithFormat:@"%ld%%", (long)progressInt];
break;
}
break;
case 2:
if ([showResultType isEqual:@"download"]) {
serverLoc.textLabel.text = [self.ResultTitles objectAtIndex:indexPath.row];
serverLoc.detailTextLabel.text = [self.downloadResults objectAtIndex:indexPath.row];
break;
}
else {
serverLoc.textLabel.text = [self.ResultTitles objectAtIndex:indexPath.row];
serverLoc.detailTextLabel.text = [self.uploadResults objectAtIndex:indexPath.row];
break;
}
break;
default:
break;
}
return serverLoc;
}
回答1:
The likely problem here is that you have a single segmentControl but you added it as a subview to both the tableHeaderView and the tableView.
回答2:
I solved the problem of an unresponsive SegmentControl upon scrolling by moving the code below from the viewDidLoad method to the titleForHeaderInSection
resultsSegment.frame = CGRectMake(45, 123, 250, 40);
[self.tableView addSubview:resultsSegment];
It now looks like this:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
{
switch (section) {
case 0:
return @"";
break;
case 1:
resultsSegment.frame = CGRectMake(45, 123, 250, 40);
[self.tableView addSubview:resultsSegment];
return @"";
break;
case 2:
return @"";
break;
default:
return @"Section Count Error";
break;
}
}
来源:https://stackoverflow.com/questions/14918055/scrolling-tableview-effects-response-time-of-segmentcontrol