首页 > 解决方案 > 带有 TableView 的 Swift 4 SegmenterControl

问题描述

我建立了一个社交应用程序,现在我想做出一些漂亮的外表。我想从 segmentedCntrol 更改标签联系人/房间。我怎样才能用正确的形式做到这一点?我有两种方法...清除联系人表视图并用房间数据填充它。或者使用表格视图隐藏视图并使用新的 tableView 显示另一个视图。

一只忙碌的猫

标签: swifttableviewcustomizationuisegmentedcontrol

解决方案


cellForRowAt您应该创建 2 个 tableViewCells 并根据选定的段在它们之间切换

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if  mySegment.selectedSegmentIndex == 0
    {
        let cell = tableView.dequeueReusableCell(withIdentifier:CellIdentifier1) as!  Cell1

       // setup here

   }
   else{

        let cell = tableView.dequeueReusableCell(withIdentifier:CellIdentifier2) as!  Cell2

        // setup here
   }
}

推荐阅读