在iOS Swift中具有多个和单个选择的下拉列表视图?

时间:2018-08-12 17:08:46

标签: ios swift xcode uitableview

我有一个过滤器,该过滤器是在视图控制器的下拉表视图中完成的。下拉表视图包含三个部分,即部分1,部分2和部分3。对于部分1和部分3应该是单选,而部分2应该是多项选择。点击第1部分时,它将展开表格视图单元格;当点击第2部分时,它将展开,而第1部分将关闭扩展。

从每个部分中选择选项时,即使用户关闭也应存储,然后重新打开过滤器下拉表视图。

我有四个问题:

  1. 当用户点击其他部分时,应该自动关闭已经打开的部分吗?

  2. 表格视图是否应根据每个部分中的数字单元格调整高度和位置?

  3. 如何对三个部分进行多次选择?

  4. 即使关闭并重新打开了下拉表视图,也应该存储选定的

这是到目前为止我尝试过的所有问题的代码:

 extension HomeViewController : UITableViewDelegate, UITableViewDataSource, ExpandableHeaderViewDelegate {




func numberOfSections(in tableView: UITableView) -> Int {

    if locationListBool == true {

        return 1

    } else {

        return sectionss.count

        }

    }

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if locationListBool == true {

        return autocompleteplaceArray.count

    } else {

        return sectionss[section].category.count

      }
    }

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

    if locationListBool == true {

        return 0

    } else {

    return 30

     }

  }

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    if locationListBool == true {

        return 30

    } else {

        if (sectionss[indexPath.section].expanded) {



           return 30
        } else  {


            return 0

        }
     }

   }

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

    if locationListBool == true {

        return 0

    } else {

    return 2


    }
   }

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    if locationListBool == true {

        return nil

    } else {

    let header = ExpandableHeaderView()
    header.contentView.backgroundColor = UIColor.white
    header.customInit(title: sectionss[section].genre, section: section, delegate: self)


    return header

    }
    }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if locationListBool == true {
            let cell = tableView.dequeueReusableCell(withIdentifier: "placecell", for: indexPath) as! locationNameTableViewCell

            guard autocompleteplaceArray.count > 0 else {

                return cell
            }

            cell.locationName.text = autocompleteplaceArray[indexPath.row]



            return cell

    } else {

    let cell = dropDownTbl.dequeueReusableCell(withIdentifier: "dropDownCell", for: indexPath) as! dropDownCell

    cell.dropDownLbl.text = sectionss[indexPath.section].category[indexPath.row]
    cell.selectionStyle   = .none

    return cell

    }

    }


   func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {



  }


func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    cell.backgroundColor = UIColor.clear

    if locationListBool == true {

        let lastRowIndex = tableView.numberOfRows(inSection: 0)
        if indexPath.row == lastRowIndex - 1  {

            tableView.allowsSelection = true

        } else {

            tableView.allowsSelection = true
        }

        }

     }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if locationListBool == true {

        if let indexPath = tableView.indexPathForSelectedRow  {

            let currentCell = tableView.cellForRow(at: indexPath) as! UITableViewCell
            searchText.text = (currentCell.textLabel?.text)
            searchText.text = autocompleteplaceArray[indexPath.row]
            placeIDString = autocompletePlaceIDArray[indexPath.row]
            print("placeIDString::::\(String(describing: placeIDString))")

            if placeIDString != nil {

                getPlaceIDLatLong(placeIDs: placeIDString!)
                print("get lat long \(getPlaceIDLatLong(placeIDs: placeIDString!))")

            }

      //            PrefsManager.sharedinstance.lastlocation = searchText.text
            locationText = searchText.text
            print("locationText::::\(String(describing: locationText))")

            }


        self.locationTableList.isHidden = true

        }
        else {


        }

    }


func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {

    switch indexPath.section {
    case 0:
        if let previousIndexPath = indexPathOfSelectedRowPaidBy {
            dropDownTbl.deselectRow(at: previousIndexPath as IndexPath, animated: false)
            dropDownTbl.cellForRow(at: previousIndexPath as IndexPath)?.accessoryType = UITableViewCellAccessoryType.none
        }
        indexPathOfSelectedRowPaidBy = indexPath as NSIndexPath?
        dropDownTbl.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark

    case 1:

        dropDownTbl.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark


    default:

        break
    }

    return indexPath
   }

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

    switch indexPath.section {

    case 0:

        if let previousIndexPath = indexPathOfSelectedRowPaidBy {
            dropDownTbl.deselectRow(at: previousIndexPath as IndexPath, animated: false)
            dropDownTbl.cellForRow(at: previousIndexPath as IndexPath)?.accessoryType = UITableViewCellAccessoryType.none
        }
        indexPathOfSelectedRowPaidBy = nil

    case 1:
        dropDownTbl.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none




    default:

        break
    }

  }


func toogleSection(header: ExpandableHeaderView, section: Int) {
    sectionss[section].expanded = !sectionss[section].expanded
    dropDownTbl.beginUpdates()

    if sectionss[0].expanded{


        dropDownTbl.layer.frame = CGRect(x: 15, y: 152, width: 345, height: 300)

    } else if sectionss[1].expanded  {


        dropDownTbl.layer.frame = CGRect(x: 15, y: 152, width: 345, height: 230)

    } else if sectionss[2].expanded {

        dropDownTbl.layer.frame = CGRect(x: 15, y: 152, width: 345, height: 330)

    } else {

        dropDownTbl.layer.frame = CGRect(x: 15, y: 152, width: 345, height: 90)
    }

    for i in 0 ..< sectionss[section].category.count {



            dropDownTbl.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)

    }
    dropDownTbl.endUpdates()
}
}

展开式表格视图标题::

 import UIKit

protocol ExpandableHeaderViewDelegate {
func toogleSection(header: ExpandableHeaderView, section: Int)
 }

 class ExpandableHeaderView: UITableViewHeaderFooterView {

var delegate: ExpandableHeaderViewDelegate?
var section: Int!
var collapaseHandlerArray = [String]()

let button = UIButton()
let button2 = UIButton()

override init(reuseIdentifier: String?){
    super.init(reuseIdentifier: reuseIdentifier)
    self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(selectheaderAction)))
 }

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
 }

@objc func selectheaderAction(gestureRecognizer: UITapGestureRecognizer) {

    let cell = gestureRecognizer.view as! ExpandableHeaderView


   }

   func customInit(title: String, section: Int, delegate: ExpandableHeaderViewDelegate) {


    self.textLabel?.text = title




    self.section = section
    self.delegate = delegate

  }

override func layoutSubviews() {
    super.layoutSubviews()

    self.textLabel?.font = UIFont(name: "Nunito-Light", size: 12)
    self.textLabel?.textColor = UIColor(red: 64.0/255, green: 75.0/255, blue: 105.0/255, alpha: 1.0)
    self.contentView.backgroundColor = UIColor.white

}

 }

下拉表视图单元格:

 class dropDownCell: UITableViewCell {

@IBOutlet weak var dropDownLbl: UILabel!
@IBOutlet weak var dropDwnBtn: UIButton!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

以下是在单元格中完成选择以及在重新打开过滤器的选择被删除或更改了选择的选项之后,尚未关闭的部分(已展开的部分)的屏幕截图。排除的结果:section with selected section2 selected with multiple option section3 with selected with single selection

1 个答案:

答案 0 :(得分:3)

没有内置的支持,允许在表格视图的不同部分中选择不同数量的单元格。

但是,UITableViewDelegate协议包含功能tableView(_:willSelectRowAt:)

如果您阅读了该函数的文档,则会显示:

  

返回值

     

确认或更改所选对象的索引路径对象   行。如果需要,返回除indexPath以外的NSIndexPath对象   另一个要选择的单元格。如果您不想要该行,则返回nil   选择。

因此,您应该能够将视图控制器设置为表视图的委托,将allowMultipleSelection标志设置为true,并在提供所需选择规则的tableView(_:willSelectRowAt:)函数中实现逻辑。

尝试编写此类函数,如果遇到问题,请发布代码,告诉我们该函数无法满足您的需求,我们将尽力帮助您进行修复。