我正在尝试向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()
}
答案 0 :(得分:0)
您可以这样做:
在
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
添加条件,您将拥有:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
if( secondSectionData && secondSectionData.count > 0)
return 2
else
return 1
}
您对评论的关注:
//其他部分的细胞怎么样?
此方法将遍历所有部分和行。如何在这里获取您的数据?好吧,只需检查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
}