如何根据布尔值在 tableView 中添加第二部分 Swift
how to add a second section in a tableView depending on a bool value Swift
我正在尝试向我的 tableView 添加第二个部分。我希望能够在单元格内的复选框被按下时从第 1 部分转移行,并使其出现在第二部分,反之亦然。
我在论坛上搜索了一下,只找到了如何按日期对版块进行排序。
请检查我的评论行以了解我在哪里有问题。
这是我的
var cellItemData = [String]() //this is my main table data
var checkedCells = Array(count: cellItemData.count, repeatedValue:false) // this is where I assign false to all my unchecked rows in section 0
var secondSectionData = [String]() //this is where I store the cells for the second section
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
if section == 0 {
return cellItemData.count
}
if section == 1 {
secondSectionData.count
}
return 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> TableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TableViewCell
if checkedCells[indexPath.row] == false{
cell.checkBoxImage.setImage(UIImage(named:"unchecked"),forState:UIControlState.Normal)
} else if checkedCells[indexPath.row] == true {
cell.checkBoxImage.setImage(UIImage(named:"checked"),forState:UIControlState.Normal)
}
cell.textLabel?.text = cellItemData[indexPath.row]
// what about the cells in the other section?
cell.checkBoxImage.tag = indexPath.row
cell.checkBoxImage?.addTarget(self, action: "buttonTouched:", forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
func buttonTouched(sender: UIButton){
for item in cellItemData {
var i = 0
if checkedCells[i] == true
{
secondSectionData.append(item)
//tableView!.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection:1)], withRowAnimation: .Automatic)
//self.tableView.reloadData()
//not sure if this is the right way
i++
}
}
let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: sender.tag, inSection: 0)) as TableViewCell
cellItemData.removeAtIndex(sender.tag)
checkedCells.removeAtIndex(sender.tag)
tableView!.deleteRowsAtIndexPaths([NSIndexPath(forRow: sender.tag, inSection:0)], withRowAnimation: .Fade)
self.tableView.reloadData()
}
你可以这样做:
在
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
添加条件,您将拥有:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
if( secondSectionData && secondSectionData.count > 0)
return 2
else
return 1
}
在评论中表达您的关注:
// what about the cells in the other section?
此方法将遍历所有部分和行。如何在这里获取您的数据?好吧,只需检查 indexPath.section 是 1 还是 2。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> TableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TableViewCell
if checkedCells[indexPath.row] == false{
cell.checkBoxImage.setImage(UIImage(named:"unchecked"),forState:UIControlState.Normal)
} else if checkedCells[indexPath.row] == true {
cell.checkBoxImage.setImage(UIImage(named:"checked"),forState:UIControlState.Normal)
}
if(indexPath.section== 1)
cell.textLabel?.text = cellItemData[indexPath.row]
else
cell.textLabel?.text = secondSectionData[indexPath.row]
// what about the cells in the other section?
cell.checkBoxImage.tag = indexPath.row
cell.checkBoxImage?.addTarget(self, action: "buttonTouched:", forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
我正在尝试向我的 tableView 添加第二个部分。我希望能够在单元格内的复选框被按下时从第 1 部分转移行,并使其出现在第二部分,反之亦然。
我在论坛上搜索了一下,只找到了如何按日期对版块进行排序。
请检查我的评论行以了解我在哪里有问题。
这是我的
var cellItemData = [String]() //this is my main table data
var checkedCells = Array(count: cellItemData.count, repeatedValue:false) // this is where I assign false to all my unchecked rows in section 0
var secondSectionData = [String]() //this is where I store the cells for the second section
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
if section == 0 {
return cellItemData.count
}
if section == 1 {
secondSectionData.count
}
return 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> TableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TableViewCell
if checkedCells[indexPath.row] == false{
cell.checkBoxImage.setImage(UIImage(named:"unchecked"),forState:UIControlState.Normal)
} else if checkedCells[indexPath.row] == true {
cell.checkBoxImage.setImage(UIImage(named:"checked"),forState:UIControlState.Normal)
}
cell.textLabel?.text = cellItemData[indexPath.row]
// what about the cells in the other section?
cell.checkBoxImage.tag = indexPath.row
cell.checkBoxImage?.addTarget(self, action: "buttonTouched:", forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
func buttonTouched(sender: UIButton){
for item in cellItemData {
var i = 0
if checkedCells[i] == true
{
secondSectionData.append(item)
//tableView!.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection:1)], withRowAnimation: .Automatic)
//self.tableView.reloadData()
//not sure if this is the right way
i++
}
}
let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: sender.tag, inSection: 0)) as TableViewCell
cellItemData.removeAtIndex(sender.tag)
checkedCells.removeAtIndex(sender.tag)
tableView!.deleteRowsAtIndexPaths([NSIndexPath(forRow: sender.tag, inSection:0)], withRowAnimation: .Fade)
self.tableView.reloadData()
}
你可以这样做:
在
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
添加条件,您将拥有:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
if( secondSectionData && secondSectionData.count > 0)
return 2
else
return 1
}
在评论中表达您的关注:
// what about the cells in the other section?
此方法将遍历所有部分和行。如何在这里获取您的数据?好吧,只需检查 indexPath.section 是 1 还是 2。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> TableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TableViewCell
if checkedCells[indexPath.row] == false{
cell.checkBoxImage.setImage(UIImage(named:"unchecked"),forState:UIControlState.Normal)
} else if checkedCells[indexPath.row] == true {
cell.checkBoxImage.setImage(UIImage(named:"checked"),forState:UIControlState.Normal)
}
if(indexPath.section== 1)
cell.textLabel?.text = cellItemData[indexPath.row]
else
cell.textLabel?.text = secondSectionData[indexPath.row]
// what about the cells in the other section?
cell.checkBoxImage.tag = indexPath.row
cell.checkBoxImage?.addTarget(self, action: "buttonTouched:", forControlEvents: UIControlEvents.TouchUpInside)
return cell
}