我有一个tableView,其中显示用户创建的圈子名称,当用户选择一个圈子名称时,我想要休息的圈子名称折叠并仅显示一个圈子名称。但我不知道该怎么做。请帮助我。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    if indexPath.section == 0
    {
        // here is the code to collapse the row
    }
 }

区域== 0具有圆名称行。

最佳答案

您可以像下面那样,折叠除选定单元格以外的其他单元格,为此,创建一个新文件时,将其称为CustomCell.swift,并使其成为UITableViewCell的子类,然后添加以下代码。

class CustomCell: UITableViewCell {

  @IBOutlet weak var circleNameTextField: UILabel!
  @IBOutlet weak var holderView: UIView!
  var cellindexPath:IndexPath?
  var selectedIndexPath:IndexPath?

  func animateCell() {
      if self.selectedIndexPath?.row == self.cellindexPath?.row {
          return
      } else {
         let isUpword = (cellindexPath!.row > selectedIndexPath!.row)
        self.animateCellWithDirection(upWord: isUpword)
      }
 }

 func animateCellWithDirection(upWord:Bool) {
     var hideRect = self.bounds
     if upWord {
         hideRect.origin.y = -hideRect.size.height
     } else {
         hideRect.origin.y = hideRect.size.height
     }

     UIView.animate(withDuration: 0.5, animations:{
       self.holderView.frame = hideRect
     })
  }
}

然后您需要在情节提要中创建一个添加原型单元,如下图所示,屏幕

ios - 收起表格查看一行中的行快速选择-LMLPHP

并将类名设置为CustomCell,如图所示,然后在属性检查器中设置如下屏幕所示的重用标识符,然后再添加一个名为holderView的视图到单元格的内容视图,并在holderView内添加标签,并在customCell中创建一个outlet,如图所示在下面的图像中,查看视图层次结构,在内容视图内部还有一个视图,它包含子视图作为UILabel圈子名称标签。

ios - 收起表格查看一行中的行快速选择-LMLPHP

现在在ViewController.swift中添加以下代码,
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var aTableView: UITableView!
var selIndexPath:IndexPath?
    override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view, typically from a nib.
      //self.aTableView.register(CustomCell.self, forCellReuseIdentifier: "CUSTOM_CELL")

   }

   override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
     // Dispose of any resources that can be recreated.
   }

   func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      self.selIndexPath = indexPath
      self.aTableView.reloadData()
   }

   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      return 5
   }

   func numberOfSections(in tableView: UITableView) -> Int {
      return 1
   }

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      let cell : CustomCell? = tableView.dequeueReusableCell(withIdentifier: "CUSTOM_CELL", for: indexPath) as? CustomCell//tableView.dequeueReusableCell(withIdentifier: "CUSTOM_CELL") as? CustomCell

       cell?.circleNameTextField.text = "Circle Name"

       cell?.cellindexPath = indexPath

       if let selectedIndexPath = self.selIndexPath {
          cell?.selectedIndexPath = selectedIndexPath
          cell?.animateCell()
       }

       return cell!
   }

}

如果要尝试在单独的项目中,我发布了整个代码
最终结果如下

ios - 收起表格查看一行中的行快速选择-LMLPHP

编辑:-)
在您问的问题中,when the user select a circle name rest circle name collapse and show only one circle name就是我为什么要这样做的原因,无论如何,听到的是对ViewController.swift类的修改,只需定义一个名为dataSourceArray的数组即可保存要在表格视图中显示的所有圈子名称,
 let dataSourceArray = ["Circle Name_1","Circle Name_2","Circle Name_3","Circle Name_4","Circle Name_5"]
 override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    //self.aTableView.register(CustomCell.self, forCellReuseIdentifier: "CUSTOM_CELL")

 }

并编辑以下方法,如下所示
 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.selIndexPath = indexPath
    self.aTableView.reloadSections(IndexSet(integer: 0), with: .fade)
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return ((self.selIndexPath) != nil) ?  1 : dataSourceArray.count
 }

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell : CustomCell? = tableView.dequeueReusableCell(withIdentifier: "CUSTOM_CELL", for: indexPath) as? CustomCell//tableView.dequeueReusableCell(withIdentifier: "CUSTOM_CELL") as? CustomCell

    cell?.cellindexPath = indexPath

    if let selectedIndexPath = self.selIndexPath {
        cell?.selectedIndexPath = selectedIndexPath
        cell?.circleNameTextField.text = dataSourceArray[selectedIndexPath.row]
       // cell?.animateCell()
    } else {
       // cell?.resetCellWith(animate:false)
         cell?.circleNameTextField.text = dataSourceArray[indexPath.row]
    }
    return cell!
}

.reloadSections(IndexSet(integer: 0), with: .fade)方法中,您可以根据需要更改动画类型。

希望以上可以作为您的要求:)

编辑:2

对于上面的代码,
只需添加一个成员变量,即可控制单元格的展开和折叠,
    var expandCell:Bool = false //initially is false

并替换为以下方法,
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.expandCell = !expandCell
    if self.expandCell {
        self.selIndexPath = indexPath
    } else {
        self.selIndexPath = nil;
    }

    self.aTableView.reloadSections(IndexSet(integer: 0), with: .fade)
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if self.expandCell {
        return ((self.selIndexPath) != nil) ?  1 : dataSourceArray.count
    } else {
        return dataSourceArray.count
    }
}

08-06 01:51