使用列表的形式来实现地区选择的功能,具体样式如下图:
首先xib中贴三个tableView,分别命名为
provinceTableView
、cityTableView
和townTableView
地区的数据源我是放在了本地一个plist文件中,命名为“Address”
下面进入代码部分:
var areaDic: NSDictionary?
var provinceArray: NSArray?
var selectedArray: NSArray?
var cityArray: NSArray?
var townArray: NSArray?
var province: String?
var province: String?
var city: String?
var town: String?
override func viewDidLoad() {
super.viewDidLoad()
provinceTableView.delegate = self
provinceTableView.dataSource = self
cityTableView.delegate = self
cityTableView.dataSource = self
townTableView.delegate = self
townTableView.dataSource = self
// 获取数据,并且把省份的数据展示出来
let path = NSBundle.mainBundle().pathForResource("Address", ofType: "plist")
areaDic = NSDictionary.init(contentsOfFile: path!)
provinceArray = areaDic?.allKeys
provinceTableView.reloadData()
}
talbeview代理:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == provinceTableView {
return (provinceArray?.count)!
}
if tableView == cityTableView {
if cityArray?.count > 0 {
return (cityArray?.count)!
} else {
return 0
}
}
if tableView == townTableView {
if townArray?.count > 0 {
return (townArray?.count)!
} else {
return 0
}
}
return 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let otherCell = UITableViewCell(style: UITableViewCellStyle.Subtitle,reuseIdentifier: "cellId")
if tableView == provinceTableView {
let cell = SelectCityTableViewCell.viewFromNibForSwift()
cell.cityLabel?.text = provinceArray![indexPath.row] as? String
// 改变cell的背景颜色
cell.backgroundColor = UIColor.init(hexValue: "e0e0e0")
// 设置cell的选中背景色
cell.selectedBackgroundView = UIView()
cell.selectedBackgroundView?.backgroundColor = UIColor.whiteColor()
return cell
}
if tableView == cityTableView {
let cell = SelectCityTableViewCell.viewFromNibForSwift()
cell.cityLabel?.text = cityArray![indexPath.row] as? String
cell.cityLabel.textColor = UIColor.blackColor()
// 设置cell的选中字体颜色
cell.cityLabel.highlightedTextColor = UIColor.init(hexValue: "ff9600")
cell.selectedBackgroundView = UIView()
cell.selectedBackgroundView?.backgroundColor = UIColor.whiteColor()
return cell
}
if tableView == townTableView {
let cell = SelectCityTableViewCell.viewFromNibForSwift()
cell.cityLabel?.text = townArray![indexPath.row] as? String
cell.cityLabel.textColor = UIColor.blackColor()
cell.cityLabel.highlightedTextColor = UIColor.init(hexValue: "ff9600")
cell.selectedBackgroundView = UIView()
cell.selectedBackgroundView?.backgroundColor = UIColor.whiteColor()
return cell
}
return otherCell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if tableView == provinceTableView {
// selectedArray保存选中cell下的内容
selectedArray = areaDic?.objectForKey((provinceArray?.objectAtIndex(indexPath.row))!) as? NSArray
if selectedArray?.count > 0 {
cityArray = selectedArray?.objectAtIndex(0).allKeys
cityTableView.reloadData()
}
townArray = nil
province = provinceArray![indexPath.row] as? String
townTableView.reloadData()
}
if tableView == cityTableView {
city = cityArray![indexPath.row] as? String
if selectedArray?.count > 0 && cityArray?.count > 0 {
townArray = selectedArray?.objectAtIndex(0).objectForKey(cityArray?.objectAtIndex(indexPath.row)) as? NSArray
townTableView.reloadData()
}
}
if tableView == townTableView {
town = townArray![indexPath.row] as? String
}
}