pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

UIScrollView 中的 UITableView 使用自動布局

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

問題描述

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

為了使事情復雜化,我還插入/刪除行以提供手風琴效果,以便當用戶點擊一行時,它將顯示該行的更多詳細信息.

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

當我更改 UITableView 的內容時,我該如何實現這一點,以便調整 UIScrollView 的大小并正確布局其內容?我目前正在使用自動布局.

解決方案

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

其次,您可能需要閱讀

滾動視圖具有淺藍色背景.頂部的紅色標簽和底部的藍色標簽是滾動視圖中表格視圖的兄弟.

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

#import "ViewController.h"#import "MyTableView.h"@interface ViewController() <UITableViewDataSource, UITableViewDelegate>@結尾@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 = @"頂部標簽";topLabel.backgroundColor = [UIColor redColor];[scrollView addSubview:topLabel];UILabel *bottomLabel = [[UILabel alloc] init];bottomLabel.translatesAutoresizingMaskIntoConstraints = NO;bottomLabel.text = @"底部標簽";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]|"選項:0 指標:無意見:意見]];[查看添加約束:[NSLayoutConstraintconstraintsWithVisualFormat:@"H:|[scrollView]|"選項:0 指標:無意見:意見]];[查看添加約束:[NSLayoutConstraintconstraintsWithVisualFormat:@"V:|[topLabel][tableView][bottomLabel]|"選項:0 指標:無意見:意見]];[查看添加約束:[NSLayoutConstraintconstraintsWithVisualFormat:@"H:|[topLabel]|"選項:0 指標:無意見:意見]];[查看添加約束:[NSLayoutConstraintconstraintsWithVisualFormat:@"H:|-8-[tableView]-8-|"選項:0 指標:無意見:意見]];[查看添加約束:[NSLayoutConstraintconstraintWithItem:tableView 屬性:NSLayoutAttributeWidthrelatedBy:NSLayoutRelationEqualtoItem:視圖屬性:NSLayoutAttributeWidth乘數:1 常數:-16]];[查看添加約束:[NSLayoutConstraintconstraintsWithVisualFormat:@"H:|[bottomLabel]|"選項:0 指標:無意見:意見]];}- (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];返回單元格;}@結尾

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

這篇關于UIScrollView 中的 UITableView 使用自動布局的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

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(以編程方式設置 contentOffset 觸發 scrollViewDidScroll)
Photos app-like gap between pages in UIScrollView with pagingEnabled(使用 pagingEnabled 的 UIScrollView 中頁面之間的照片應用程序式間隙)
why UIScrollView is leaving space from top in ios 6 and ios 7(為什么 UIScrollView 在 ios 6 和 ios 7 中從頂部留下空間)
UIScrollView pauses NSTimer while scrolling(UIScrollView 在滾動時暫停 NSTimer)
主站蜘蛛池模板: 二手电脑回收_二手打印机回收_二手复印机回_硒鼓墨盒回收-广州益美二手电脑回收公司 | 线材成型机,线材折弯机,线材成型机厂家,贝朗自动化设备有限公司1 | 芝麻黑-芝麻黑石材厂家-永峰石业| 手表腕表维修保养鉴定售后服务中心网点 - 名表维修保养 | 免费B2B信息推广发布平台 - 推发网| 河北凯普威医疗器材有限公司,高档轮椅系列,推车系列,座厕椅系列,协步椅系列,拐扙系列,卫浴系列 | 上海新光明泵业制造有限公司-电动隔膜泵,气动隔膜泵,卧式|立式离心泵厂家 | 真空干燥烘箱_鼓风干燥箱 _高低温恒温恒湿试验箱_光照二氧化碳恒温培养箱-上海航佩仪器 | 贵州科比特-防雷公司厂家提供贵州防雷工程,防雷检测,防雷接地,防雷设备价格,防雷产品报价服务-贵州防雷检测公司 | 深圳天际源广告-形象堆头,企业文化墙,喷绘,门头招牌设计制作专家 | 有机肥设备生产制造厂家,BB掺混肥搅拌机、复合肥设备生产线,有机肥料全部加工设备多少钱,对辊挤压造粒机,有机肥造粒设备 -- 郑州程翔重工机械有限公司 | 焊接烟尘净化器__焊烟除尘设备_打磨工作台_喷漆废气治理设备 -催化燃烧设备 _天津路博蓝天环保科技有限公司 | 冲锋衣滑雪服厂家-冲锋衣定制工厂-滑雪服加工厂-广东睿牛户外(S-GERT) | 油漆辅料厂家_阴阳脚线_艺术漆厂家_内外墙涂料施工_乳胶漆专用防霉腻子粉_轻质粉刷石膏-魔法涂涂 | 深圳彩钢板_彩钢瓦_岩棉板_夹芯板_防火复合彩钢板_长鑫 | 免费个人pos机申请办理-移动pos机刷卡-聚合收款码办理 | 便携式表面粗糙度仪-彩屏硬度计-分体式粗糙度仪-北京凯达科仪科技有限公司 | 网站优化公司_SEO优化_北京关键词百度快速排名-智恒博网络 | 衢州装饰公司|装潢公司|办公楼装修|排屋装修|别墅装修-衢州佳盛装饰 | 高速混合机_锂电混合机_VC高效混合机-无锡鑫海干燥粉体设备有限公司 | 检验科改造施工_DSA手术室净化_导管室装修_成都特殊科室建设厂家_医疗净化工程公司_四川华锐 | 撕碎机,撕破机,双轴破碎机-大件垃圾破碎机厂家| 圣才学习网-考研考证学习平台,提供万种考研考证电子书、题库、视频课程等考试资料 | NM-02立式吸污机_ZHCS-02软轴刷_二合一吸刷软轴刷-厦门地坤科技有限公司 | GAST/BRIWATEC/CINCINNATI/KARL-KLEIN/ZIEHL-ABEGG风机|亚喜科技 | 【黄页88网】-B2B电子商务平台,b2b平台免费发布信息网 | BOE画框屏-触摸一体机-触控查询一体机-触摸屏一体机价格-厂家直销-触发电子 | 广东健伦体育发展有限公司-体育工程配套及销售运动器材的体育用品服务商 | 青岛球场围网,青岛车间隔离网,青岛机器人围栏,青岛水源地围网,青岛围网,青岛隔离栅-青岛晟腾金属制品有限公司 | 金属管浮子流量计_金属转子流量计厂家-淮安润中仪表科技有限公司 | 无菌检查集菌仪,微生物限度仪器-苏州长留仪器百科 | 高低温试验房-深圳高低温湿热箱-小型高低温冲击试验箱-爱佩试验设备 | 欧美日韩国产一区二区三区不_久久久久国产精品无码不卡_亚洲欧洲美洲无码精品AV_精品一区美女视频_日韩黄色性爱一级视频_日本五十路人妻斩_国产99视频免费精品是看4_亚洲中文字幕无码一二三四区_国产小萍萍挤奶喷奶水_亚洲另类精品无码在线一区 | 黄石妇科医院_黄石东方女子医院_黄石东方妇产医院怎么样 | 农产品溯源系统_农产品质量安全追溯系统_溯源系统 | 大型低温冷却液循环泵-低温水槽冷阱「厂家品牌」京华仪器_京华仪器 | 粉末包装机-给袋式包装机-全自动包装机-颗粒-液体-食品-酱腌菜包装机生产线【润立机械】 | 塑胶地板-商用PVC地板-pvc地板革-安耐宝pvc塑胶地板厂家 | 汽车水泵_汽车水泵厂家-瑞安市骏迪汽车配件有限公司 | 武汉不干胶印刷_标签设计印刷_不干胶标签印刷厂 - 武汉不干胶标签印刷厂家 | 拼装地板,悬浮地板厂家,悬浮式拼装运动地板-石家庄博超地板科技有限公司 |