久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

UIScrollView 中的 UITableView 使用自動(dòng)布局

UITableView within UIScrollView using autolayout(UIScrollView 中的 UITableView 使用自動(dòng)布局)
本文介紹了UIScrollView 中的 UITableView 使用自動(dòng)布局的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

目前,我正在使用 UITableView 以及 UIScrollView 中包含的其他視圖.我希望 UITableView 的高度與其內(nèi)容高度相同.

為了使事情復(fù)雜化,我還插入/刪除行以提供手風(fēng)琴效果,以便當(dāng)用戶點(diǎn)擊一行時(shí),它將顯示該行的更多詳細(xì)信息.

我已經(jīng)完成了插入/刪除,但目前它沒有更新作為其父視圖的 UIScrollView,以便重新計(jì)算 UIScrollView 的內(nèi)容大小和 UITableViewUIScrollView 中的其他視圖一起正確顯示.

當(dāng)我更改 UITableView 的內(nèi)容時(shí),我該如何實(shí)現(xiàn)這一點(diǎn),以便調(diào)整 UIScrollView 的大小并正確布局其內(nèi)容?我目前正在使用自動(dòng)布局.

解決方案

首先,那些其他視圖(table view 的兄弟姐妹)是否嚴(yán)格地在 table view 的上方和下方?如果是這樣,您是否考慮過讓表格視圖正常滾動(dòng),并將這些外部視圖放在表格視圖的頁眉和頁腳視圖中?那么你就不需要滾動(dòng)視圖了.

其次,您可能需要閱讀

滾動(dòng)視圖具有淺藍(lán)色背景.頂部的紅色標(biāo)簽和底部的藍(lán)色標(biāo)簽是滾動(dòng)視圖中表格視圖的兄弟.

這是我測(cè)試中視圖控制器的完整源代碼.沒有xib文件.

#import "ViewController.h"#import "MyTableView.h"@interface ViewController() <UITableViewDataSource, UITableViewDelegate>@結(jié)尾@implementation 視圖控制器-(無效)加載視圖{UIView *view = [[UIView alloc] init];self.view = 視圖;UIScrollView *scrollView = [[UIScrollView alloc] init];scrollView.translatesAutoresizingMaskIntoConstraints = NO;scrollView.backgroundColor = [UIColor cyanColor];[查看 addSubview:scrollView];UILabel *topLabel = [[UILabel alloc] init];topLabel.translatesAutoresizingMaskIntoConstraints = NO;topLabel.text = @"頂部標(biāo)簽";topLabel.backgroundColor = [UIColor redColor];[scrollView addSubview:topLabel];UILabel *bottomLabel = [[UILabel alloc] init];bottomLabel.translatesAutoresizingMaskIntoConstraints = NO;bottomLabel.text = @"底部標(biāo)簽";bottomLabel.backgroundColor = [UIColor blueColor];[scrollView addSubview:bottomLabel];UITableView *tableView = [[MyTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];tableView.translatesAutoresizingMaskIntoConstraints = NO;tableView.dataSource = 自我;tableView.delegate = self;[scrollView addSubview:tableView];UILabel *footer = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];footer.backgroundColor = [UIColor greenColor];footer.text = @"頁腳";tableView.tableFooterView = 頁腳;NSDictionary *views = NSDictionaryOfVariableBindings(scrollView, topLabel, bottomLabel, tableView);[查看添加約束:[NSLayoutConstraintconstraintsWithVisualFormat:@"V:|[scrollView]|"選項(xiàng):0 指標(biāo):無意見:意見]];[查看添加約束:[NSLayoutConstraintconstraintsWithVisualFormat:@"H:|[scrollView]|"選項(xiàng):0 指標(biāo):無意見:意見]];[查看添加約束:[NSLayoutConstraintconstraintsWithVisualFormat:@"V:|[topLabel][tableView][bottomLabel]|"選項(xiàng):0 指標(biāo):無意見:意見]];[查看添加約束:[NSLayoutConstraintconstraintsWithVisualFormat:@"H:|[topLabel]|"選項(xiàng):0 指標(biāo):無意見:意見]];[查看添加約束:[NSLayoutConstraintconstraintsWithVisualFormat:@"H:|-8-[tableView]-8-|"選項(xiàng):0 指標(biāo):無意見:意見]];[查看添加約束:[NSLayoutConstraintconstraintWithItem:tableView 屬性:NSLayoutAttributeWidthrelatedBy:NSLayoutRelationEqualtoItem:視圖屬性:NSLayoutAttributeWidth乘數(shù):1 常數(shù):-16]];[查看添加約束:[NSLayoutConstraintconstraintsWithVisualFormat:@"H:|[bottomLabel]|"選項(xiàng):0 指標(biāo):無意見:意見]];}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {返回 20;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];如果(!單元格){cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];}cell.textLabel.text = [NSString stringWithFormat:@"Row %d", indexPath.row];返回單元格;}@結(jié)尾

At the moment, I'm using a UITableView along with other views that are contained in a UIScrollView. I want the UITableView to have its height to be the same as its content height.

To complicate things, I'm also inserting / deleting rows to provide an accordion effect so that when the user taps on a row, it will show more detail for that row.

I've got the insert / deletion done, though at the moment it doesn't update the UIScrollView which is its superview so that the content size of the UIScrollView is recalculated and the UITableView along with other views in the UIScrollView are displayed correctly.

How can I go about implementing this so that UIScrollView's size is adjusted and its contents laid out correctly when I change the content of the UITableView? I'm currently using auto layout.

解決方案

First of all, are those other views (siblings of the table view) strictly above and below the table view? If so, have you considered letting the table view scroll normally, and putting those outside views in the table view's header and footer views? Then you don't need the scroll view.

Second, you may want to read Technical Note TN2154: UIScrollView And Autolayout if you haven't already.

Third, given the information in that tech note, I can think of a few ways to do what you want. The cleanest is probably to create a subclass of UITableView that implements the intrinsicContentSize method. The implementation is trivial:

@implementation MyTableView

- (CGSize)intrinsicContentSize {
    [self layoutIfNeeded]; // force my contentSize to be updated immediately
    return CGSizeMake(UIViewNoIntrinsicMetric, self.contentSize.height);
}

@end

Then just let auto layout use the table view's intrinsic content size. Create the constraints between the subviews of the scroll view (including the table view) to lay them out, and make sure there are constraints to all four edges of the scroll view.

You probably need to send invalidateIntrinsicContentSize to the table view at appropriate times (when you add or remove rows or change the heights of rows). You could probably just override the appropriate methods in MyTableView to do that. E.g. do [self invalidateIntrinsicContentSize] in -endUpdates, -reloadData, - insertRowsAtIndexPaths:withRowAnimation:, etc.

Here's the result of my testing:

The scroll view has the light blue background. The red top label and the blue bottom label are siblings of the table view inside the scroll view.

Here's the complete source code for the view controller in my test. There's no xib file.

#import "ViewController.h"
#import "MyTableView.h"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

@end

@implementation ViewController

- (void)loadView {
    UIView *view = [[UIView alloc] init];
    self.view = view;

    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    scrollView.backgroundColor = [UIColor cyanColor];
    [view addSubview:scrollView];

    UILabel *topLabel = [[UILabel alloc] init];
    topLabel.translatesAutoresizingMaskIntoConstraints = NO;
    topLabel.text = @"Top Label";
    topLabel.backgroundColor = [UIColor redColor];
    [scrollView addSubview:topLabel];

    UILabel *bottomLabel = [[UILabel alloc] init];
    bottomLabel.translatesAutoresizingMaskIntoConstraints = NO;
    bottomLabel.text = @"Bottom Label";
    bottomLabel.backgroundColor = [UIColor blueColor];
    [scrollView addSubview:bottomLabel];

    UITableView *tableView = [[MyTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.translatesAutoresizingMaskIntoConstraints = NO;
    tableView.dataSource = self;
    tableView.delegate = self;
    [scrollView addSubview:tableView];

    UILabel *footer = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    footer.backgroundColor = [UIColor greenColor];
    footer.text = @"Footer";
    tableView.tableFooterView = footer;

    NSDictionary *views = NSDictionaryOfVariableBindings(
        scrollView, topLabel, bottomLabel, tableView);
    [view addConstraints:[NSLayoutConstraint
        constraintsWithVisualFormat:@"V:|[scrollView]|"
        options:0 metrics:nil views:views]];
    [view addConstraints:[NSLayoutConstraint
        constraintsWithVisualFormat:@"H:|[scrollView]|"
        options:0 metrics:nil views:views]];
    [view addConstraints:[NSLayoutConstraint
        constraintsWithVisualFormat:@"V:|[topLabel][tableView][bottomLabel]|"
        options:0 metrics:nil views:views]];
    [view addConstraints:[NSLayoutConstraint
        constraintsWithVisualFormat:@"H:|[topLabel]|"
        options:0 metrics:nil views:views]];
    [view addConstraints:[NSLayoutConstraint
        constraintsWithVisualFormat:@"H:|-8-[tableView]-8-|"
        options:0 metrics:nil views:views]];
    [view addConstraint:[NSLayoutConstraint
        constraintWithItem:tableView attribute:NSLayoutAttributeWidth
        relatedBy:NSLayoutRelationEqual
        toItem:view attribute:NSLayoutAttributeWidth
        multiplier:1 constant:-16]];
    [view addConstraints:[NSLayoutConstraint
        constraintsWithVisualFormat:@"H:|[bottomLabel]|"
        options:0 metrics:nil views:views]];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"Row %d", indexPath.row];
    return cell;
}

@end

這篇關(guān)于UIScrollView 中的 UITableView 使用自動(dòng)布局的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to subclass UIScrollView and make the delegate property private(如何繼承 UIScrollView 并使委托屬性私有)
Swift - how to get last taken 3 photos from photo library?(Swift - 如何從照片庫中獲取最后拍攝的 3 張照片?)
Setting contentOffset programmatically triggers scrollViewDidScroll(以編程方式設(shè)置 contentOffset 觸發(fā) scrollViewDidScroll)
Photos app-like gap between pages in UIScrollView with pagingEnabled(使用 pagingEnabled 的 UIScrollView 中頁面之間的照片應(yīng)用程序式間隙)
why UIScrollView is leaving space from top in ios 6 and ios 7(為什么 UIScrollView 在 ios 6 和 ios 7 中從頂部留下空間)
UIScrollView pauses NSTimer while scrolling(UIScrollView 在滾動(dòng)時(shí)暫停 NSTimer)
主站蜘蛛池模板: 亚洲性爰 | 国产美女久久久 | 免费在线观看一区二区 | 亚洲丝袜天堂 | 91视视频在线观看入口直接观看 | 精品中文字幕视频 | 99色播| 亚洲综合电影 | 91精品一区| 成人超碰在线 | 日本成人福利 | 亚洲色图插插插 | 亚洲免费在线视频 | 色婷婷综合久久久中文字幕 | 91资源在线观看 | 亚洲情综合五月天 | 国产午夜精品一区二区三区在线观看 | 精品欧美黑人一区二区三区 | 日韩不卡一区二区 | 久草.com| 99reav | 午夜视频在线免费观看 | 中文字幕视频在线观看 | 欧美激情视频网站 | 日韩精品久久久久 | 欧美亚洲视频在线观看 | www.亚洲.com | 国产成人精品久久二区二区91 | 国产一区二区三区精品久久久 | 欧美极品在线视频 | 亚洲在线视频 | 久久久久久久av | aⅴ色国产 欧美 | 亚洲精品成人在线 | 亚洲一区二区在线视频 | 欧美福利视频 | 中国美女av | 欧美日韩一区精品 | 成人小视频在线观看 | 欧美日韩不卡合集视频 | 久久国产精品一区二区三区 |