swift tableview如何选择所有行 [英] swift tableview how to select all rows
问题描述
我在表格视图中有一个按钮,当我按下该按钮时,它将选择所有单元格行,该怎么做?我尝试了很多,但一无所获
I have Button in tableview I want when I press that button will select all cell rows, how to do that? I tried alot but I got nothing
我很困惑如何使按钮与单元格接触
I'm so confused how to make the button contact the cell
我试图像这样制作var
I've tried to make var like this
var x = false
那我喜欢
if( x == true ) { //Code }
当您按下按钮时,它将为真
and when you press the button it will be true
但是我不知道该放在哪个函子中?而且我不知道如何选择所有行
but I don't know in which func I should put this ? and I don't know how to select all rows
有人可以帮助我如何选择\按下表格视图中的按钮时取消选择单元格中的所有行.
Could someone help me how to Select \ Deselect all rows in cell when pressing button in tableview.
非常感谢您!
推荐答案
var selectedUsernameArray : NSMutableArray = []
var username1Array : NSMutableArray = ["hel","dsf","fsdg"]//just a sample
最初的按钮名称应为全选"
Initially button name should be "Select all"
按钮动作:
@IBAction func buttonAction(sender: UIButton) {
if(sender.titleLabel?.text == "Select all")
{
selectedUsernameArray .addObjectsFromArray(username1Array as [AnyObject])
sender.titleLabel?.text = "Deselect all"
}
else
{
selectedUsernameArray .removeAllObjects();
sender.titleLabel?.text = "Select all"
}
self.tableView .reloadData()
}
tableview委托
tableview delegate
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
var ob: AnyObject = username1Array .objectAtIndex(indexPath.row);
cell.textLabel?.text = ob as? String;
if(selectedUsernameArray .containsObject(ob))
{
cell.backgroundColor = UIColor.blueColor();
}
else
{
cell.backgroundColor = UIColor.whiteColor();
}
return cell
}
这篇关于swift tableview如何选择所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!