滚动TableView会影响SegmentControl的响应时间

时间:2013-02-17 04:43:12

标签: ios6 scroll addsubview uisegmentedcontrol nstableheaderview

这可能是一个奇怪的问题,我希望以前有人拥有它。

我使用以下代码向我的TableView添加了一个SegmentControl:

   UIView *headerView = [[UIView alloc] init ];
   [headerView addSubview:resultsSegment];
    self.tableView.tableHeaderView = headerView;

    resultsSegment.frame = CGRectMake(45, 123, 250, 40);
    [self.tableView addSubview:resultsSegment];

在这些论坛的帮助下,前三行使得TableView标题的Segment部分成为滚动的原因。大。

但是,这会禁用点击SegmentControl的功能。

添加最后一行再次成为可能。

SegmentControl执行很棒的UNTIL滚动,然后它变得非常无响应。它不会引起任何错误,它最终会接受来自手指的按压,但你必须在它切换之前点击它5/6次。

如果有人能够对此有所了解,那就太棒了

您需要的任何额外信息我很乐意提供!

编辑----

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;

选择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;

}

2 个答案:

答案 0 :(得分:0)

这里可能存在的问题是你有一个segmentControl,但你把它作为子视图添加到tableHeaderView和tableView。

答案 1 :(得分:0)

通过将以下代码从viewDidLoad方法移动到titleForHeaderInSection

,我解决了滚动时无响应的SegmentControl问题
 resultsSegment.frame = CGRectMake(45, 123, 250, 40);
            [self.tableView addSubview:resultsSegment];

现在看起来像这样:

- (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;
    }


}