Swift Tableview How to Select All Rows

Select ALL TableView Rows Programmatically Using selectRowAtIndexPath

At the time allJobsSelected becomes true, you need to call the UITableView method selectRowAtIndexPath(_:animated:scrollPosition:) for each row of your table. In my case, I attached this functionality to the right bar button item which I named Select All. Calling this from cellForRowAtIndexPath is surely not the right place.

@IBAction func doSelectAll(sender: UIBarButtonItem) {
let totalRows = tableView.numberOfRowsInSection(0)
for row in 0..<totalRows {
tableView.selectRowAtIndexPath(NSIndexPath(forRow: row, inSection: 0), animated: false, scrollPosition: UITableViewScrollPosition.None)
}
}

Swift: Multiple TableView checkmark - select all rows

You have to check if the user selected the first row ("Select all") and update the other rows accordingly:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// toggle the selected area
areas[indexPath.row].isSelected.toggle()

// save the new state for later use
let isSelected = areas[indexPath.row].isSelected

if indexPath.row == 0 {
// "Select all" was selected – update all areas
for i in 1..<areas.count {
areas[i].isSelected = isSelected
}

// update UI
tableView.visibleCells.forEach { $0.accessoryType = isSelected ? .checkmark : .none }
} else {
// update UI
tableView.cellForRow(at: indexPath)?.accessoryType = isSelected ? .checkmark : .none
}

tableView.deselectRow(at: indexPath, animated: true)
}

Recommendation

To separate concerns visually you could also use an own table view section for the "Select all" row. In that case some more changes are necessary:

var areas = [
// you do not need an area for "Select all" any longer
Area(name: "a"),
Area(name: "b"),
Area(name: "c"),
Area(name: "d")
]

var allSelected: Bool {
// helper var to see if all areas are currently selected
return areas.filter({!$0.isSelected}).isEmpty
}

override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch section {
case 1: return "Areas"
default: return nil
}
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0: return 1 // select all
case 1: return areas.count
default:
// we should never get here
fatalError()
}
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.selectionStyle = .none

if indexPath.section == 0 {
cell.textLabel?.text = "Select all"
cell.accessoryType = allSelected ? .checkmark : .none
} else {
let area = areas[indexPath.row]
cell.textLabel?.text = area.name
cell.accessoryType = area.isSelected ? .checkmark : .none
}

return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
// (de-)select all
let shouldSelect = !allSelected
for i in 0..<areas.count {
areas[i].isSelected = shouldSelect
}
} else {
areas[indexPath.row].isSelected.toggle()
}

tableView.reloadRows(at: tableView.indexPathsForVisibleRows ?? [], with: .automatic)
}

swift tableview how to select all rows

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

Initially button name should be "Select all"

Button action :

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

How to select all rows and get its id in tableview when i click button outside of tableview in swift

You can use Swift map function, by it you get all ids in your selectedArray

@IBAction func selectAllBtn(_ sender: UIButton) {
let servicesCount = self.serviceList?.result?.services?.count ?? 0
if servicesCount == self.arrSelectedRows {
self.arrSelectedRows.removeAll()
} else {
self.arrSelectedRows = self.serviceList?.result?.services?.map({$0.id ?? 0})
}
tableView.reloadData()
}

Selecting every row in TableView including off-screen rows

You don't.

You can't select all cells because they're being reused, which means only enough cells exist.

When the "Acknowledge" button is pressed, you can get the data from your datasource, the one you're consuming to create your UITableView.


Note: If there is a state change when that button is pressed, you should iterate through your datasource and update your objects. And then you .reloadData().


Updates based on your question update, this is how you iterate through your datasource.

        var dataClassArr = [DataClass]()

var result = [String]()

dataClassArr.append(DataClass(id: "1"))
dataClassArr.append(DataClass(id: "42")) // just for example, you should remove those.

for element in dataClassArr {
result.append(element.id)
}

print(result) // ["1", "42"] -> based on example values.

select all the rows in the rows in the tableview programatically in ios using swift on a button click?

In the button action you should modify your datasource and then call tableView.reloadData() which will reload the table using the updated datasource.



Related Topics



Leave a reply



Submit