gpt4 book ai didi

ios - 如何将 UITableView 与具有 UISwitch 的单元格一起使用并为全选添加一个开关?

转载 作者:行者123 更新时间:2023-11-28 12:42:11 26 4
gpt4 key购买 nike

This is the UI that I designed

这是第二个屏幕 2nd image file

第三个屏幕

3rd Image

如果您在图像中注意到表格 View 中的第一项是全选,并且该单元格中有一个相应的 Uiswitch

我想让 tableview 中的其余所有 uiSwitch 在切换全选时切换。

我的 View Controller 代码是

import UIKit
import Alamofire
import SwiftyJSON

protocol SettingCellDelegate : class {
func didChangeSwitchState(sender: CustomerTableViewCell, isOn: Bool)
}

class CutomerListViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,SettingCellDelegate {

@IBOutlet weak var CustomerTableView: UITableView!

var refreshControl: UIRefreshControl!

var mList = NSMutableOrderedSet()

let cellSpacingHeight: CGFloat = 8

var cId: [String] = []

override func viewDidLoad() {
super.viewDidLoad()

//print("selected customer")
//print (cId)
getListOfCustomer()
// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

var mCustomerList : [Customer] = []

func getListOfCustomer(){

//print("Getting List Of Woises")
let ud = NSUserDefaults.standardUserDefaults()

let decoded = ud.objectForKey("userObject") as! NSData
let user = NSKeyedUnarchiver.unarchiveObjectWithData(decoded) as! UserObject

let url = Request.getListCustomer()+user.CompanyID
print(url)
Alamofire.request(.GET, url)
.validate()
.responseJSON { (_ ,_ ,result) in
switch result {
case .Success(let data):
let jsonResponse = JSON(data)
//print("JSON")
print(jsonResponse)
var mCustomer: Customer

self.mCustomerList.append(Customer(CustomerID: "0", CustomerName: "Select All", Username: "", Password: "", IsActive: "", ContactPersonEmail: "", ContactPersonPhone: "", CompanyID: "", CustomerRevenue: "", ContactPerson: "", CustomerURL: "", Country: "", Logo: "", CustomerPriority: "", CustomerSatisfaction: "", CustomerStatus: "", CustomerCreatedOn: "", CustomerRegion: "", CreatedBy: "", UpdatedBy: "", ID: ""))
for i in 0 ..< jsonResponse["document"].count {


mCustomer = Customer(
CustomerID: jsonResponse["document"][i]["CustomerID"].description,
CustomerName: jsonResponse["document"][i]["CustomerName"].description,
Username: jsonResponse["document"][i]["Username"].description,
Password: jsonResponse["document"][i]["Password"].description,
IsActive: jsonResponse["document"][i]["IsActive"].description,
ContactPersonEmail: jsonResponse["document"][i]["ContactPersonEmail"].description,
ContactPersonPhone: jsonResponse["document"][i]["ContactPersonPhone"].description,
CompanyID: jsonResponse["document"][i]["CompanyID"].description,
CustomerRevenue: jsonResponse["document"][i]["CustomerRevenue"].description,
ContactPerson: jsonResponse["document"][i]["ContactPerson"].description,
CustomerURL: jsonResponse["document"][i]["CustomerURL"].description,
Country: jsonResponse["document"][i]["Country"].description,
Logo: jsonResponse["document"][i]["Logo"].description,
CustomerPriority: jsonResponse["document"][i]["CustomerPriority"].description,
CustomerSatisfaction: jsonResponse["document"][i]["CustomerSatisfaction"].description,
CustomerStatus: jsonResponse["document"][i]["CustomerStatus"].description,
CustomerCreatedOn: jsonResponse["document"][i]["CustomerCreatedOn"].description,
CustomerRegion: jsonResponse["document"][i]["CustomerRegion"].description,
CreatedBy: jsonResponse["document"][i]["CreatedBy"].description,
UpdatedBy: jsonResponse["document"][i]["UpdatedBy"].description,
ID: jsonResponse["document"][i]["ID"].description)

self.mCustomerList.append(mCustomer)
self.mList.addObjectsFromArray(self.mCustomerList)

}

print("CUSTOMER Count"+String(self.mList.count))
if(self.CustomerTableView != nil){
self.CustomerTableView.reloadData()
}

/* print("mlist printed from fetchNotification API")
print(self.mList)
print("mList count from fetchNotification API")
print(self.mList.count)*/
case .Failure(let error):
print("Request failed with error: \(error)")
}
}



}


func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return self.cellSpacingHeight
}

internal func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return mList.count
}

internal func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
CustomerTableView = tableView
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomerTableViewCell

if mList.count > 0{
let mCustomer = mList.objectAtIndex(indexPath.row) as! Customer

cell.Title?.text = mCustomer.CustomerName

if self.cId.contains(mCustomer.CustomerID) || self.cId.contains(" " + mCustomer.CustomerID){
cell.SelectionStatus.setOn(true, animated:true)
}else{
cell.SelectionStatus.setOn(false, animated:true)
}
cell.cellDelegate = self

}
return cell

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}

func didChangeSwitchState(sender: CustomerTableViewCell, isOn: Bool) {
let indexPath = self.CustomerTableView.indexPathForCell(sender)




}
/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

}

我的 UITAbleview 单元格的代码是

import UIKit

class CustomerTableViewCell: UITableViewCell {

@IBOutlet weak var Title: UILabel!
@IBOutlet weak var SelectionStatus: UISwitch!
weak var cellDelegate: SettingCellDelegate?
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
}





@IBAction func handledSwitchChange(sender: UISwitch) {
self.cellDelegate?.didChangeSwitchState(self, isOn:SelectionStatus.on)
print("select all uday")
}



}

请帮助我,我是 iOS 开发的新手

最佳答案

  • Customer 类/结构中添加属性 selected
  • 根据selected更新cellForRowAtIndexPath中开关的状态
  • didChangeSwitchState 中,如果索引路径不为 0,则更新相应 Customer 对象的该属性,如果索引路径为 0,则设置 all 选择数据源数组中的属性并重新加载 TableView 。

关于ios - 如何将 UITableView 与具有 UISwitch 的单元格一起使用并为全选添加一个开关?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39171242/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com