【问题标题】:Swift how to move tableView backGroundView labelSwift如何将tableView移回GroundView标签
【发布时间】:2017-05-01 02:45:39
【问题描述】:

我有一个 tableView 有:

1-) 第 1 节标题

    TableView Cell

2-) 第 2 节标题

    Tableview Cell

3-) 第 3 节

    (n rows that contains list of records)

我的 tableView 有 3 个部分。在第 3 节中,我列出了记录。当没有记录时,我会显示一个标签,上面写着“未找到记录”我的表格视图,如下图所示。

My Table View Picture

但我的问题是,我想将标签移动到 tableview 的底部(在“+”视图上)我该如何移动这个标签。这是我的快速代码

func setNoDataInfoIfAbsenceNotExists()
{
    let noDataLabel : UILabel = UILabel()
    noDataLabel.frame = CGRectMake(0, 0 , (self.tableView.bounds.width), (self.tableView.bounds.height))
    noDataLabel.text = "No Records Found"
    noDataLabel.textColor = UIColor.blackColor()
    noDataLabel.textAlignment = .Center
    self.tableView.backgroundView = noDataLabel
    self.tableView.separatorStyle = .None
}

【问题讨论】:

  • 你为什么不使用storyboard来改变约束???
  • 在“+”视图上是什么意思?你的意思是制作+视图的标签子视图还是点击+你想移动标签???
  • nodataLabel.frame = (+View).frame 试试这个...

标签: ios iphone swift ios7


【解决方案1】:

您正在将框架 noDataLabel 设置为表格视图的大小,并且文本位于该框架的中心。

您应该尝试缩小框架(尝试 CGRectMake(0, 0, 0, 0) 并在设置文本后调用 noDataLabel.sizeToFit()),然后使用 noDataLabel.center.xnoDataLabel.center.y 定位它。

试试:

noDataLabel.text = "No Records Found"
noDataLabel.sizeToFit()
noDataLabel.center.x = tableView.center.x
noDataLabel.center.y = tableView.frame.height - noDataLabel.frame.height

【讨论】:

  • noDataLabel.text = "未找到记录" noDataLabel.sizeToFit() noDataLabel.center.x = tableView.center.x noDataLabel.center.y = tableView.frame.height -noDataLabel.frame.height我添加了这一行,但标签仍在同一位置
  • 你试过了吗:noDataLabel.frame = CGRectMake(0, 0 , 0, 0)?
  • 让 noDataLabel : UILabel = UILabel() noDataLabel.frame = CGRectMake(0, 0 , 0,0) noDataLabel.text = "未找到记录" noDataLabel.sizeToFit() noDataLabel.center.x = tableView.center.x noDataLabel.center.y = tableView.frame.height - noDataLabel.frame.height 是的,我试过了
  • 只是为了确定,你可以添加(在那个函数的任何地方):dump(tableView.frame) 并检查 tableView 的帧大小是否正确?
  • ▿ (0.0, 72.0, 414.0, 616.0) ▿ 原点:(0.0, 72.0) - x: 0.0 - y: 72.0 ▿ 尺寸:(414.0, 616.0) - 宽度:414.0 - 高度:616.0这是转储结果
【解决方案2】:

添加这一行

tableView.tableFooterView  = tableViewFooter

你得到了这样的结果。在 textview 底部查看蓝色视图 .

您的视图只保留您的 tableview 高度,并且您的视图始终位于 tableview 的末尾。

【讨论】:

    【解决方案3】:

    创建新单元格,其中将包含带有文本“No Results”(或您需要的任何内容)的标签。 在您的代码中:

    numberOfRowsInSection

    //other part of your implementation
    if indexPath.section == 3 {
        return dataArray.isEmpty ? 1 : dataArray.count
    }
    

    cellForRowAtIndexPath方法内:

    // other part of your implementation
    if indexPath.section == 3 {
        guard !dataArray.isEmpty else {
            return NoDataCell() // replace with your implementation
        }         return YourDataCell()
    }
    

    【讨论】:

    • 但在这种情况下,“无数据单元格”将是可选择的并且表现得像一个单元格。我认为使用单元格作为页脚是一个更好的主意。不是吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-31
    相关资源
    最近更新 更多