Checkboxes TableView


#1
 // Override to support rearranging the table view.
    func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
        
        let elementToMove = ordersArray[fromIndexPath.row]
        ordersArray.remove(at: fromIndexPath.row)
        ordersArray.insert(elementToMove, at: to.row)
    }

     func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
        return .none
    }
    
     func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
        return false
    }
    
    // Override to support conditional rearranging of the table view.
    func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    
    // called when about to proceed segue
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        // if the ID is "ToDetailViewController" -> create a constant *destination that stores a destionController / DetailViewController
        if segue.identifier == "TodetailOrderVC", let destination = segue.destination as? detailOrderVC {
            
            // accessing a presssed cell and getting indexPath of the pressed cell
            if let cell = sender as? UITableViewCell, let indexPath = tableView.indexPath(for: cell) {
                
                 //let arr = ordersArray[indexPath.row]
                
                destination.allAboutOrders = someorder_id[indexPath.row]
                destination.allAboutOrdersid = someid[indexPath.row]
                destination.allAboutOrdersStatus = somestatus[indexPath.row]
                destination.allAboutOrdersDate1 = somedate1[indexPath.row]
                destination.allAboutOrdersDate2 = somedate2[indexPath.row]
                
                //Мутка без пробельщина
                
                let contacts = someContacs[indexPath.row]
                let telBezProbel = contacts.replacingOccurrences(of: " ", with: "")
                destination.allAboutTargetContacs = telBezProbel
             
                destination.allAboutPriceclient = somePriceclient[indexPath.row]
                destination.allAboutTargetname = someTargetname[indexPath.row]
                destination.allAboutAdmnotes = someAdmnotes[indexPath.row]
                destination.allAboutTargetnotes = someTargetnotes[indexPath.row]
                destination.allAboutAddr = someAddr[indexPath.row]
                
                //print("someContacs=\(telBezProbel)")
                
                destination.allAboutChekCard = someCheckCard[indexPath.row]
                
                
            }
        }
    }

Как запилить чекбокс и запихать его в массив

далее я хочу передать это дело в post запрос


#2
    tableView.allowsMultipleSelection = true

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)
    cell?.accessoryType = .checkmark
///тут добавлять indexPath в массив
}


func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)
    cell?.accessoryType = .none
///тут убирать indexPath из массива
}

#3
 let elementToMove = someorder_id[fromIndexPath.row]
        someorder_id.remove(at: fromIndexPath.row)
        someorder_id.insert(elementToMove, at: to.row)

меня интересует someorder_id к примеру


#4

Что? Я не экстрасенс.


#5

добавить в массив someorder_id который равен [“ls111”, “ls222”, “ls333”]


#6
 var someArr = [[String]]()
 someArr.append(someorder_id)

Это?


#7

смотри в someorder_id приходят все значения а мне нужны только выбранные

 var someArr = [[String]]()
        someArr.append(someorder_id)
        
        print("МАССИВ=\(someArr)")

вот что возвращает когда я выбираю
МАССИВ=[[“LS2-123”, “LS2-124”, “LS2-125”]]


#8

я выбираю скажем LS2-123
то он должен поместить в массив его, если я с него снимаю выборку то массив остается пустым


#9

Как вариант:

let selectedRows = tableView.indexPathsForSelectedRows 

Вот и получается массив со всеми выделенными row.Ну или вместо IndexPath можно сохранять IndexPath.row, т.е. Int значение - тут уже по необходимости. Дальше - просто.


#10

someorder_id ghb[jlbn приходит как string соответсвенно консоль материться


#11

Изменил ответ на более правильный.
Конечно ругается, у вас же someorder_id - это массив данных, из которых заполняются rows.
Вам нужно из этого массива достать все данные по индексам в selectedRows (см ответ выше).

P.S. someorder_id - так называть переменные нельзя - не понятно что находится внутри, никаких подчеркиваний в переменных быть не должно.


#12

let selectedRows = tableView.indexPathsForSelectedRows

не понятно куда константа эта встает …


#13

Туда, где вам нужно получить все выделенные rows.


#14
//
//  partialTableVC.swift
//  Logsis
//
//  Created by yakuza on 17.01.17.
//  Copyright © 2017 yakuza. All rights reserved.
//

import UIKit

class partialTableVC: UITableViewController {
    
    
    var array = ["1","2","3","4"]

   
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.allowsMultipleSelection = true

        
    }
    
 
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return array.count
    }

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

        cell.textLabel!.text = array[indexPath.row]

        return cell
    }
   

   
    // Override to support rearranging the table view.
//    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
//
//    }
//   
//    // Override to support conditional rearranging of the table view.
//    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
//        // Return false if you do not want the item to be re-orderable.
//        return true
//    }
    
    
//        override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//            let cell = tableView.cellForRow(at: indexPath)
//            cell?.accessoryType = .checkmark
//            ///тут добавлять indexPath в массив
//    
//    
//        }
//    
//        override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
//            let cell = tableView.cellForRow(at: indexPath)
//            cell?.accessoryType = .none
//            ///тут убирать indexPath из массива
//        }

    
    
    var array = IndexPath
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath)
        cell?.accessoryType = .checkmark
        
        array.append(indexPath)
    }
    
    override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath)
        cell?.accessoryType = .none
        
        for i in 0..< array.count {
            if array[i] == indexPath {
                array.remove(at: i)
            }
        }
    }
    
    
    
}

вот пример на котором пробую


#15

не получается поместить в массив и удалить из него

даже поместить не получается


#16

я разобрался по своему через батоны