如何在UITableView - Swift中制作清单?

时间:2016-05-09 14:48:48

标签: ios swift uitableview

我用ConnectionOptions cOption = new ConnectionOptions(); ManagementScope scope = new ManagementScope("\\\\" + machine + "\\" + nameSpaceRoot + "\\" + managementScope, cOption); scope.Options.Username = UserName; scope.Options.Password = passWord; scope.Options.EnablePrivileges = true; scope.Options.Authentication = AuthenticationLevel.PacketPrivacy; //scope.Options.Timeout = TimeSpan.FromSeconds(180); //cOption.Timeout = TimeSpan.FromSeconds(180); scope.Options.Impersonation = ImpersonationLevel.Impersonate; scope.Connect(); return scope; if (scope.IsConnected && scope != null) { query = new ObjectQuery(@"Select * from Win32_SCSIController"); searcher = new ManagementObjectSearcher(scope, query); searcher.Options.Timeout = new TimeSpan(0, 0, wbemConnectFlagUseMaxWait); ManagementObjectCollection qWin32_SCSIController = searcher.Get(); foreach (ManagementObject item in qWin32_SCSIController) { <Some code here> } 创建了一个应用。我想在触摸单元格时在左侧显示刻度线标记,并在触摸单元格时隐藏。我使用了一些代码并且它没有按我的意愿显示。勾号显示在右侧而不是屏幕左侧。

这就是我想要的方式: enter image description here

就是这样: enter image description here

以下是我使用的代码:

UITableView

总之,第一个问题是刻度线显示在单元格的右侧而不是左侧。另一个问题是,它不会解开(再次按下细胞时隐藏)

感谢。

2 个答案:

答案 0 :(得分:4)

试试这个

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // Table view cells are reused and should be dequeued using a cell identifier.
        let cellIdentifier = "DhikrTableViewCell"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! GoalsTableViewCell

        cell.tickButton.addTarget(self, action: #selector(GoalsViewController.toggleSelcted(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        cell.tickButton.tag = indexPath.row
        // Fetches the appropriate meal for the data source layout.
        let workout = workouts[indexPath.row]
        let number = numbers[indexPath.row]

        if workout.isSelected {
            cell.tickButton.setImage(UIImage(named: "Ticked Button"), forState: UIControlState.Normal)
        } else {
            cell.tickButton.setImage(UIImage(named: "Tick Button"), forState: UIControlState.Normal)
        }

        cell.nameLabel.text = workout.name
        cell.numberLabel.text = number.number

        return cell
    }

func toggleSelcted(button: UIButton) {
        let workout = workouts[button.tag]
        workout.isSelected = !workout.isSelected
        myTableView.reloadData()
    }

答案 1 :(得分:1)

要使用默认的UITableViewCell实现此行为,您可能需要将表视图设置为编辑模式。

在viewDidLoad中尝试以下代码。

self.tableView.allowsMultipleSelectionDuringEditing = true
self.tableView.setEditing(true, animated: false)

这将在左侧显示刻度线,并且当再次按下单元格时,这将取消勾选(隐藏)。

编辑: 如果您需要更多自定义,请创建自定义表格视图单元格并手动处理选择/刻度标记。

这将是你的cell.swift。

class CustomCell: UITableViewCell {

@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var tickImageView: UIImageView!

//Handles the cell selected state
var checked: Bool! {
    didSet {
        if (self.checked == true) {
            self.tickImageView.image = UIImage(named: "CheckBox-Selected")
        }else{
            self.tickImageView.image = UIImage(named: "CheckBox-Normal")
        }
    }
}

override func awakeFromNib() {
    super.awakeFromNib()
    checked = false
    self.layoutMargins = UIEdgeInsetsZero
    self.separatorInset = UIEdgeInsetsZero
}

数据源数组是

let list = ["Title 1", "Title 2", "Title 2", "Title 4"]

视图控制器didLoad就像

    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    listView.registerNib(UINib(nibName: "OptionsSelectionCell", bundle: nil), forCellReuseIdentifier: "SelectionCell")
}

视图控制器表委托方法将类似于

extension ViewController: UITableViewDataSource, UITableViewDelegate {

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("SelectionCell") as! OptionsSelectionCell
    cell.titleLabel.text = list[indexPath.row]
    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath) as! OptionsSelectionCell
    cell.checked = !cell.checked
}
}