swift tableview如何选择所有行

标签 swift uitableview button

我在 tableview 中有一个按钮,我希望当我按下该按钮时将选择所有单元格行,该怎么做?我尝试了很多,但我一无所获

我很困惑如何让按钮接触单元格

我试过像这样制作var

var x = false

那我就喜欢

if( x == true ) { //Code }

当你按下按钮时它会是真的

但我不知道应该把它放在哪个函数中?而且我不知道如何选择所有行

谁能帮我在表格 View 中按下按钮时如何选择\取消选择单元格中的所有行。

非常感谢!

最佳答案

var selectedUsernameArray : NSMutableArray = []
var username1Array : NSMutableArray = ["hel","dsf","fsdg"]//just a sample

最初的按钮名称应该是“全选”

按钮操作:

 @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()
}

表格 View 代理

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如何选择所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32045424/

相关文章:

swift - 如何检查句子是否包含特定单词?

ios - Calendar.Component 中的季度组件有什么作用? ( swift )

Delphi:自定义标题栏中的自定义按钮 - 类似 Firefox/Opera 主菜单按钮

java - Android 按钮的动画效果不符合预期

swift - 检查 Swift 中是否存在函数

swift 4 : Implementation of a generic protocol with protocol as an associated type

ios - 如何像 iPhone 内置闹钟应用程序标记其闹钟名称一样来标记该行?

swift - RxSwift : Ambiguous reference to member 'items(cellIdentifier:cellType:)'

ios - 如何将 UITableViewCell 水平拖动到左侧?

ios - 值不发送到下一个 UIViewController