我有一个tableView,其中我显示用户创建的圆圈名称,我想当用户选择一个圆圈名称休息圆圈名称折叠并只显示一个圆圈名称。但我不知道该怎么做。请帮帮我们。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
if indexPath.section == 0
{
// here is the code to collapse the row
}
}
Section == 0包含圆圈名称行。
答案 0 :(得分:2)
你可以这样做,为了折叠除了所选单元格之外的其他单元格,为此创建一个新文件,我们可以将其称为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
})
}
}
你需要在故事板中创建一个原型单元格,如下图所示,屏幕
并将类名设置为CustomCell
,如图所示,并在属性检查器中设置重用标识符,如下面的屏幕,还有一件事将名为holderView
的视图添加到单元格的内容视图并添加标签在holderView
内并在customCell中创建outlet
,如下图所示,查看视图层次结构,在内容视图中还有一个视图,它包含子视图UILabel
圈子名称标签
现在在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!
}
}
如果想在单独的项目中试用,我发布了整个代码 最终结果如下,
修改: - )强>
在你问的问题中,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
}
}
答案 1 :(得分:1)
首先,我制作了一个用于管理展开折叠功能的结构。 所有宏:
let IndexVal = -4
let ISSELECTED = "ISSELECTED"
let SELECTED = "SELECTED"
let UNSELECTED = "UNSELECTED"
let FLAGE = "FLAGE"
let DATA = "DATA"
我创建了一个数组arrTA = NSMutableArray()
,其中将存储整个处理过的数据。
这里我使用了arrFolderList,其中存储了JSON数据。
现在,当您调用API并在该响应中时,您需要使用IndexVal
self.reloadTableForExpandCollapse(IndexVal)
这将设置默认视图,即collpased视图。
以这种方式使用它:
func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.reloadTableForExpandCollapse(indexPath.row)
}
现在功能定义:
func reloadTableForExpandCollapse( indexValue: Int) {
var index = indexValue
if(index == IndexVal) { /// This is the case when we run first time
arrTA = NSMutableArray()
for i in 0..<self.arrFolderList.count {
let strFlage = "MA"
let dict = NSMutableDictionary()
dict.setObject(strFlage, forKey: FLAGE)
dict.setObject(UNSELECTED, forKey: ISSELECTED)
dict.setObject(self.arrFolderList[i] , forKey: DATA)
arrTA.addObject(dict)
}
} else { /// When we are performing the operations
/// When we collapsed
if(index == IndexVal) {
arrTA = NSMutableArray()
for i in 0..<self.arrFolderList.count {
let strFlage = "MA"
let dict = NSMutableDictionary()
dict.setObject(strFlage, forKey: FLAGE)
dict.setObject(UNSELECTED, forKey: ISSELECTED)
dict.setObject(self.arrFolderList[i], forKey: DATA)
arrTA.addObject(dict)
}
self.tblView_Expandable.reloadData()
} else {
if(index < arrTA.count) {
let dict = arrTA.objectAtIndex(index) as! NSMutableDictionary
dict.setObject(SELECTED, forKey: ISSELECTED)
let strFlage = dict[FLAGE] as! String
if(strFlage == "MA") { /// Means it is main array
var isAdding: Bool = true
var isRemoving: Bool = true
if (index < self.arrTA.count) {
for var j in index + 1..<self.arrTA.count { /// removed + 1from index
let dict1 = self.arrTA[j]
let strFlage1 = (dict1[FLAGE] as! String)
if (strFlage1 == "SA") && isRemoving {
isAdding = false
dict[ISSELECTED] = UNSELECTED
self.arrTA.removeObjectAtIndex(j)
j -= 1
break
} else if isAdding {
isRemoving = false
break
} else if (strFlage1 == "MA") && isRemoving {
break
}
}
}
if ((dict[FLAGE] as! String) == "MA") {
var k = 0
while k < self.arrTA.count {
let dict1 = self.arrTA[k] as! NSMutableDictionary
dict1[ISSELECTED] = UNSELECTED
let strFlage1 = (dict1[FLAGE] as! String)
if (strFlage1 == "SA") {
self.arrTA.removeObjectAtIndex(k)
if k < index {
index -= 1
}
k -= 1
}
k += 1
}
}
if isAdding {
let dict = self.arrTA[index] as! NSMutableDictionary
let strFlage = (dict[FLAGE] as! String)
// dict[ISSELECTED] = UNSELECTED
self.arrTA.replaceObjectAtIndex(index, withObject: dict)
if (strFlage == "MA") {
let dictData = (dict[DATA] as! NSDictionary)
let dictSA = NSMutableDictionary()
let strFlage = "SA"
dictSA[FLAGE] = strFlage
dictSA[DATA] = dictData
dictSA[ISSELECTED] = UNSELECTED
self.arrTA.insertObject(dictSA, atIndex: index + 1)
}
if(self.tblView_Expandable.lastIndexpath().row == index) {
self.scrollTableViewToBottom()
}
}
}else { }
}
}
}
if(tblView_Expandable != nil) {
tblView_Expandable.reloadData()
}
}
我希望这会有所帮助.... :)
答案 2 :(得分:0)
为折叠的circel名称单元格自定义其他单元格。 例如CollapsedCell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
if let cell = tableView.dequeueReusableCell(withIdentifier: "kCollapsedCell", for: indexPath) as CollapsedCell {
cell.doSomething()
}
}
}