Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何将UITableView始终放在UIView的中心?_Iphone_Ios_Uitableview - Fatal编程技术网

Iphone 如何将UITableView始终放在UIView的中心?

Iphone 如何将UITableView始终放在UIView的中心?,iphone,ios,uitableview,Iphone,Ios,Uitableview,我想在UIView的中心实现一个UITableView。最初它只有2或3行。当用户添加更多行时,它将沿垂直方向延伸,而整个内容仍保留在中心,如下所示: 是否可以使用UITableView?可以使用UIScrollView的contentOffset属性来完成 使tableView的框架位于边界内: tableView.frame = self.view.bounds; tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UI

我想在
UIView
的中心实现一个
UITableView
。最初它只有2或3行。当用户添加更多行时,它将沿垂直方向延伸,而整个内容仍保留在中心,如下所示:


是否可以使用
UITableView

可以使用UIScrollView的
contentOffset
属性来完成

  • 使tableView的框架位于边界内:

    tableView.frame = self.view.bounds;
    tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    
  • 声明一个方法-layoutTableView:

    - (void)layoutTableView {
        CGSize contentSize = tableView.contentSize;
        CGSize boundsSize = tableView.bounds.size;
        CGFloat yOffset = 0;
        if(contentSize.height < boundsSize.height) {
            yOffset = floorf((boundsSize.height - contentSize.height)/2);
        }
        tableView.contentOffset = CGPointMake(0, yOffset);
    }
    
    -(无效)layoutTableView{
    CGSize contentSize=tableView.contentSize;
    CGSize boundsSize=tableView.bounds.size;
    CGFloat yOffset=0;
    if(contentSize.height
  • 调用
    [tableView reloadData]
    后,只需调用
    [self layoutTableView]


  • 如果在tableview中不使用标题,则可以动态计算单元格的高度与tableview绑定高度的比较

    荣誉

    -(CGFloat)表格视图:(UITableView*)表格视图头部高度部分:(NSInteger)部分{
    CGFloat内容高度=0.0;
    对于(int section=0;section<[self numberOfSectionsInTableView:tableView];section++){
    对于(int row=0;row<[self tableView:tableView numberofrowsinssection:section];row++){
    nsindepath*indepath=[nsindepath indexPathForRow:row-instation:section];
    contentHeight+=[self-tableView:tableView-heightforrowatinexpath:indexPath];
    }
    }
    返回值(tableView.bounds.size.height-contentHeight)/2;
    }
    -(UIView*)表格视图:(UITableView*)表格视图用于标题部分:(NSInteger)部分{
    UIView*view=[[UIView alloc]initWithFrame:CGRectZero];
    view.backgroundColor=[UIColor clearColor];
    返回视图;
    }
    
    另一个解决方案是调整表视图的内容插入,因为内容偏移解决方案不适合我。以下是基本思想(插入到自定义UITableView子类中):

    -(void)重新加载数据{
    [超级重载数据];
    [self-CenterTableViewContentSifRequired];
    }
    -(无效)布局子视图{
    [超级布局子视图];
    [self-CenterTableViewContentSifRequired];
    }
    -(无效)需要中心表视图内容{
    CGFloat totalHeight=CGRectGetHeight(self.bounds);
    CGFloat contentHeight=self.contentSize.height;
    //如果我们的内容少于我们的桌子框架,那么我们可以居中
    BOOL CONTENTCANBECENTER=内容高度<总高度;
    如果(可以输入内容){
    self.contentInset=UIEdgeInsetsMake(ceil(totalHeight/2.f-contentHeight/2.f),0,0,0);
    }否则{
    self.contentInset=UIEdgeInsetsZero;
    }
    }
    
    对于心地敏捷的人,这里有一个游乐场:

    import UIKit
    import Foundation
    import XCPlayground
    
    class CenteredTable: UITableView {
        override func reloadData() {
            super.reloadData()
            centerTableContentsIfNeeded()
        }
    
        override func  layoutSubviews() {
            super.layoutSubviews()
            centerTableContentsIfNeeded()
        }
    
        func centerTableContentsIfNeeded() {
            let totalHeight = CGRectGetHeight(bounds)
            let contentHeight = contentSize.height
            let contentCanBeCentered = contentHeight < totalHeight
            if (contentCanBeCentered) {
                contentInset = UIEdgeInsets(top: ceil(totalHeight/2 - contentHeight/2), left: 0, bottom: 0, right: 0);
            } else {
                contentInset = UIEdgeInsetsZero;
            }
        }
    }
    
    class DataSource: NSObject, UITableViewDataSource {
        let items = ["Mr", "Anderson", "Welcome", "Back", "We", "Missed", "You"]
        func registerReusableViewsWithTable(tableView: UITableView) {
            tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
        }
    
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
            cell.textLabel?.text = items[indexPath.row]
            cell.textLabel?.textAlignment = .Center
            return cell
        }
    
        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return items.count
        }
    }
    
    let dataSource = DataSource()
    let table = CenteredTable(frame: CGRectMake(0, 0, 300, 800), style: UITableViewStyle.Plain)
    table.tableFooterView = UIView(frame: CGRectZero)
    let container = UIView(frame: table.frame)
    container.addSubview(table)
    dataSource.registerReusableViewsWithTable(table)
    table.dataSource = dataSource
    table.reloadData()
    XCPShowView("table", container)
    container
    
    导入UIKit
    进口基金会
    导入运动场
    以类为中心的表:UITableView{
    重写func重载数据(){
    super.reloadData()
    centerTableContentsIfNeeded()
    }
    覆盖func布局子视图(){
    super.layoutSubviews()
    centerTableContentsIfNeeded()
    }
    func CenterTableContentSifRequired(){
    设totalHeight=CGRectGetHeight(边界)
    设contentHeight=contentSize.height
    让contentcanbecenter=contentHeightUITableView单元格{
    让cell=tableView.dequeueReusableCellWithIdentifier(“cell”,forIndexPath:indexPath)作为!UITableViewCell
    cell.textlab?.text=项[indexPath.row]
    单元格.textLabel?.textAlignment=.Center
    返回单元
    }
    func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
    返回项目。计数
    }
    }
    让dataSource=dataSource()
    let table=CenteredTable(框架:CGRectMake(0,0,300,800),样式:UITableViewStyle.Plain)
    table.tableFooterView=UIView(帧:CGRectZero)
    let container=UIView(框架:table.frame)
    container.addSubview(表)
    dataSource.registerReusableViewsWithTable(表)
    table.dataSource=数据源
    表.重新加载数据()
    XCPShowView(“表格”,容器)
    容器
    
    Swift 3

    private func hightTableView() {
        yourTableView.frame = CGRect(x: 0, y: Int((Int(view.frame.height) - rowHeight * yourArrayData.count) / 2), width: widthTable, height: rowHeight * yourArrayData.count)
    }
    

    没有必要有那么庞大的逻辑。只需在
    cellForRowAt()中使用以下行即可

    斯威夫特:

    tableView.contentInset = UIEdgeInsets(top: (tableView.bounds.height/2 - cell.bounds.height/2), left: 0, bottom: 0, right: 0)
    
    目标C:

    tableView.contentInset = UIEdgeInsetsMake((tableView.bounds.height/2 - cell.bounds.height/2), 0, 0, 0);
    

    请看以下内容:这不是问题的答案,它调整了框架。在我的示例中,框架可以等于
    self.view.bounds
    。谢谢kashiv,但最初它可能只有两行,当您将其框架定义为self.view.bounds时。行中可能没有显示行吗?OK,您可以调用<代码> [自LayOutTabVIEW ] < /C> >代码> > TabLVIEW:WyDeStudioCyto:FoRoWaveRexXPATT:<代码> >我想 TabelVIEW。它使内容在滚动后返回中心,其中
    contentOffset
    只是更改滚动的位置<代码>tableView.contentInset=UIEdgeInsetsMake(yOffset,0.f,yOffset,0.f)tableView.contentInset = UIEdgeInsets(top: (tableView.bounds.height/2 - cell.bounds.height/2), left: 0, bottom: 0, right: 0)
    tableView.contentInset = UIEdgeInsetsMake((tableView.bounds.height/2 - cell.bounds.height/2), 0, 0, 0);