首页 文章

重新加载表视图后出现最后一个静态单元格分隔线

提问于
浏览
0

我正在使用下面的代码来摆脱最后一个静态单元格底部分隔线,它适用于我不需要重新加载tableView的情况 . 一旦我重新加载我的tableView分隔线出现 .

只有超级VC中的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    let footerFrame = CGRect(x: 0.0, y: 0.0, width: 
    tableView.frame.size.width, height: 1.0)
    tableView.tableFooterView = UIView(frame: footerFrame)
}

并致电:

fileprivate func showDatePicker()
{
    datePicker.setValue(UIColor.white, forKey: "textColor")
    showsDatePicker = true

    tableView.beginUpdates()
    tableView.endUpdates()

    tableView.contentInset = UIEdgeInsets(top: -20.0, left: 0.0, 
    bottom: 0.0, right: 0.0)
}

在我孩子的tableView(didSelectRowAt indexPath:_)中我只是用日期选择器更改单元格的高度:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    guard showsDatePicker else {
       switch indexPath.row {
        case CellIndex.withDatePicker:
            return 0.0
        default:
            return 44.0 
        }
    }

    switch indexPath.row {
        case CellIndex.withDatePicker:
            return 217.0
        default:
            return 44.0 
        }
}

之后我画了分隔线!也许这是因为STATIC单元格的使用,唯一的方法是在storyboard中为每个viewController添加空白视图?将 tableView.tableFooterView = UIView() 替换为 viewDid/WillLayoutSubviews 会导致空屏幕效果 . 我不想在故事板中为我的所有控制器添加一个空白视图,也许有一种方法可以通过编程方式进行操作?

2 回答

  • 0

    在显示最后一个单元格之前

    if indexPath.row == Your_last_Cell {
    
      cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0);
    
    }
    

    创建具有最小高度的页脚:

    func tableView(_ tableView: UITableView, 
    heightForFooterInSection section: Int) -> CGFloat{
    
    // This will create a "invisible" footer
         return 0.01f; //CGFLOAT_MIN
    }
    

    要么

    func tableView(_ tableView: UITableView, 
     viewForFooterInSection section: Int) -> UIView?{
    
    UIView(frame: CGRectZero)
    
    }
    
  • 0

    我获得这个结果的方法如下,

    在viewDidLoad函数中,

    override func viewDidLoad() {
        super.viewDidLoad()
        // Add this line
        yourTableView.tableFooterView = UIView()
    }
    

    定义表视图的节数

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2
    }
    

    在你的cellForRowAtIndexPath函数中,

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell: UITableViewCell?
    switch indexPath.section {
        case 0:
            cell = tableView.dequeueReusableCellWithIdentifier("yourfirstcell")
            if indexPath.row == yourLastRowIndex {
            self.separatorInset = UIEdgeInsetsMake(0, self.bounds.size.width, 0, 0)
        }
        case 1:
            cell = tableView.dequeueReusableCellWithIdentifier("yoursecondcell")
        default:
            break
        }
        return cell!
    }
    

    这种方法对您有所帮助 .

相关问题