swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.03k stars 10.32k forks source link

I want table view multiple row selection with a fav image in Swift #66492

Closed saiful2396 closed 1 year ago

saiful2396 commented 1 year ago

I have an array of countries lists that I show in my table view now I want to select those countries in which the first letter is A/B/C... etc. when I select those rows containing the first letter "A" then I don't want to select other countries which first letter contains B/C/D... etc. the problem is when I select those countries which first letter contain "A" selected fine but others rows are also auto-select that I don't want. here is my code please check it and help me thanks in advance.

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var myTable: UITableView!

var countries:[String] = ["Afghanistan","Albania","Algeria","Andorra","Angola","Anguilla","Antigua & Barbuda","Argentina","Armenia","Aruba","Australia","Austria","Azerbaijan","Bahamas","Bahrain","Bangladesh","Barbados","Belarus","Belgium","Belize","Benin","Bermuda","Bhutan","Bolivia","Bosnia & Herzegovina","Botswana","Brazil","British Virgin Islands","Brunei","Bulgaria","Burkina Faso","Burundi","Cambodia","Cameroon","Cape Verde","Cayman Islands","Chad","Chile","China","Colombia","Congo","Cook Islands","Costa Rica","Cote D Ivoire","Croatia","Cruise Ship","Cuba","Cyprus","Czech Republic","Denmark","Djibouti","Dominica","Dominican Republic","Ecuador","Egypt","El Salvador","Equatorial Guinea","Estonia","Ethiopia","Falkland Islands","Faroe Islands","Fiji","Finland","France","French Polynesia","French West Indies","Gabon","Gambia","Georgia","Germany","Ghana","Gibraltar","Greece","Greenland","Grenada","Guam","Guatemala","Guernsey","Guinea","Guinea Bissau","Guyana","Haiti","Honduras","Hong Kong","Hungary","Iceland","India","Indonesia","Iran","Iraq","Ireland","Isle of Man","Israel","Italy","Jamaica","Japan","Jersey","Jordan","Kazakhstan","Kenya","Kuwait","Kyrgyz Republic","Laos","Latvia","Lebanon","Lesotho","Liberia","Libya","Liechtenstein","Lithuania","Luxembourg","Macau","Macedonia","Madagascar","Malawi","Malaysia","Maldives","Mali","Malta","Mauritania","Mauritius","Mexico","Moldova","Monaco","Mongolia","Montenegro","Montserrat","Morocco","Mozambique","Namibia","Nepal","Netherlands","Netherlands Antilles","New Caledonia","New Zealand","Nicaragua","Niger","Nigeria","Norway","Oman","Pakistan","Palestine","Panama","Papua New Guinea","Paraguay","Peru","Philippines","Poland","Portugal","Puerto Rico","Qatar","Reunion","Romania","Russia","Rwanda","Saint Pierre & Miquelon","Samoa","San Marino","Satellite","Saudi Arabia","Senegal","Serbia","Seychelles","Sierra Leone","Singapore","Slovakia","Slovenia","South Africa","South Korea","Spain","Sri Lanka","St Kitts & Nevis","St Lucia","St Vincent","St. Lucia","Sudan","Suriname","Swaziland","Sweden","Switzerland","Syria","Taiwan","Tajikistan","Tanzania","Thailand","Timor L'Este","Togo","Tonga","Trinidad & Tobago","Tunisia","Turkey","Turkmenistan","Turks & Caicos","Uganda","Ukraine","United Arab Emirates","United Kingdom","Uruguay","Uzbekistan","Venezuela","Vietnam","Virgin Islands (US)","Yemen","Zambia","Zimbabwe"]

var sectionTitle = [String]()

var countryDict = [String: [String]]()
var favDic = [Int:Bool]()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    sectionTitle = Array(Set(countries.compactMap({String($0.prefix(1))})))

    sectionTitle.sort()

    for title in sectionTitle {
        countryDict[title] = [String]()
    }

    for country in countries {
        countryDict[String(country.prefix(1))]?.append(country)
    }

    blankFavList ()
}

func blankFavList ()
{
    for i in 0...countries.count
    {
        favDic[i] = false
    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return countryDict[sectionTitle[section]]!.count
}

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

    if favDic[indexPath.row] == true
    {
        cell.lblName?.text = countryDict[sectionTitle[indexPath.section]]?[indexPath.row]
        cell.mySelect.image = UIImage(named: "chk")
    }
    else
    {
        cell.lblName?.text = countryDict[sectionTitle[indexPath.section]]?[indexPath.row]
        cell.mySelect.image = UIImage(named: "unchk")
    }

    return cell
}

func numberOfSections(in tableView: UITableView) -> Int {
    return sectionTitle.count
}

func sectionIndexTitles(for tableView: UITableView) -> [String]? {
    return sectionTitle
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return sectionTitle[section]
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let cell = myTable.cellForRow(at: indexPath) as! MyTableViewCell

    //if mobileList[indexPath.row] == "Samsung"
    if sectionTitle[indexPath.section] == "A"
    {
        cell.mySelect.image = UIImage(named: "chk")
        favDic[indexPath.row] = true
        print(sectionTitle[indexPath.section])
        print(countryDict[sectionTitle[indexPath.section]]?[indexPath.row] as Any)
    }
    else
    {
        let alert = UIAlertController(title: "Message", message: "Please Select only those country which first letter is A", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default))

        self.present(alert, animated: true, completion: nil)
    }
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {

    let cell = myTable.cellForRow(at: indexPath) as! MyTableViewCell

    //if mobileList[indexPath.row] == "Samsung"
    if sectionTitle[indexPath.section] == "A"
    {
        cell.mySelect.image = UIImage(named: "unchk")
        favDic[indexPath.row] = false
        print(sectionTitle[indexPath.section])
        print(countryDict[sectionTitle[indexPath.section]]?[indexPath.row] as Any)
    }
    else
    {
        let alert = UIAlertController(title: "Message", message: "Please Select only those country which first letter is A", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default))

        self.present(alert, animated: true, completion: nil)
    }

}
}

output image which i select

second image auto select

3rd image for validation

AnthonyLatsis commented 1 year ago

Hello Saiful, this repository accepts issues with the Swift compiler or Standard Library. Please use a more dedicated space such as StackOverflow or the Apple developer forums for questions about using closed-source Apple frameworks.