簡體   English   中英

測試UITableView的部分頁腳

[英]Testing UITableView's section footer

嘗試在由UITableViewDelegate方法創建的UITableView中測試UISegmentedControl:

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 0) {
        UIView *container = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, [self tableView:tableView heightForFooterInSection:section])];
        [self.segmentedControl setCenter:container.center];
        [container addSubview:self.segmentedControl];
        return container;
    } else {
        return [super tableView:tableView viewForFooterInSection:section];
    }
}

在測試課中:

-(void)testSegmentedControl {
    MyTableViewController *viewController = [[MyTableViewController alloc]initWithNibName:@"MyTableViewController" bundle:nil];
    [viewController.tableView reloadData];

    // Getting the footer via the delegate is cheating IMO.
    UITableViewHeaderFooterView *footer =  [viewController.tableView footerViewForSection:0];
    UISegmentedControl *segmentControl = footer.subviews[0];
    // do stuff to the segmentControl then check the tableView.
    [viewController.tableView reloadData];
}

我目前正在通過全局屬性( viewController.segmentedControl )操縱UISegmentedControl ,然后調用[viewController.tableView reloadData]更新UITableViewCell的狀態。 但是在我看來,從[viewController.tableView footerViewForSection:0]獲取頁腳是正確的測試方法。 任何指導表示贊賞。

編輯:

正如約翰·羅傑斯(John Rodgers)所建議的那樣,我已經嘗試像在cellForRowAtIndexPath:那樣使UITableViewHeaderFooterView出隊cellForRowAtIndexPath:

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 0) {
        UITableViewHeaderFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:self.footerReuseId];
        if (footer == nil) {
            footer = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:self.footerReuseId];
        }
        if (![footer.subviews containsObject:self.segmentedControl]) {
            [footer addSubview:self.segmentedControl];
        }
        return footer;
    } else {
        return [super tableView:tableView viewForFooterInSection:section];
    }
}

測試方法無改進:

-(void)testSegmentedControl {
    MyTableViewController *viewController = [[MyTableViewController alloc]initWithNibName:@"MyTableViewController" bundle:nil];
    [viewController.tableView reloadData];
    UITableViewHeaderFooterView *footer1 = (UITableViewHeaderFooterView *)[viewController.tableView dequeueReusableHeaderFooterViewWithIdentifier:viewController.footerReuseId];
    UITableViewHeaderFooterView *footer2 = [viewController.tableView footerViewForSection:0];
    // Break point shows that footer1 and footer2 are nil.
}

您在這里遇到的問題是, footerViewForSection實際上是一個UITableView方法,用於為其提供該部分的視圖。

您將必須使可重用的UITableViewHeaderFooterView出隊,以便在測試類中對其進行測試:

UITableViewHeaderFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:mySectionFooterViewIdentifier];

這正是為表視圖提供footerViewForSection方法中應該使用的方法,而只是在測試類中將其出隊。

希望這可以幫助!

〜J

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM