Swift - 从图像变化的列表中选择多个

时间:2014-11-29 11:38:47

标签: ios xcode uitableview swift

我对此非常陌生,现在已经苦苦挣扎了几个星期,想出一个从列表中选择多个项目的解决方案。如果选择左侧图像,则选择该线条并将图像更改为“选定”图像。如果用户点击“i”,它将转到详细信息屏幕。我在第二个屏幕上显示了我想要实现的目标...... 一旦选择了相关的项目,我将有一个提交按钮将列表中的那些项目发送到某个地方...我想我将需要在某处使用allowsMultipleSelection,然后将选择存储到一个数组中并防止segue到如果未选择“i”,则显示详细信息屏幕。

以下是我想要实现的截图 - ScreenSamples

这是我用来获取第一个未选择列表的代码...在ViewController类中:UIViewController,UITableViewDataSource。

let repeatList = [("Item 1",    "Sub 1"),
    ("Item 2",  "Sub 2"),
    ("Item 3",  "Sub 3"),
    ("Item 4",  "Sub 4"),
    ("Item 5",  "Sub 5")]

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

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
    let (repeatProduct, repeatCount) = repeatList[indexPath.row]

    cell.textLabel.text = repeatProduct
    cell.detailTextLabel?.text = repeatCount

        //retrieve an image
    var repeatSelectorImage = UIImage(named: "checkedIcon")
    cell.imageView.image = repeatSelectorImage

        return cell
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "Repeatable Items"
}


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

}

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

1 个答案:

答案 0 :(得分:0)

这就是我所做的。

import UIKit

class CheckButton: UIButton {

  //images
  let checkedImage = UIImage(named: "checkedIcon")
  let unCheckedImage = UIImage(named: "unCheckedIcon")

  //bool property
  var isChecked:Bool = false{
    didSet{
        if isChecked == true{
            self.setImage(checkedImage, forState: .Normal)
        } else {
            self.setImage(unCheckedImage, forState: .Normal)
        }
    }
  }

  override func awakeFromNib() {
    self.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
    self.isChecked = false
  }

  func buttonClicked(sender:UIButton!) {
    if(sender == self) {
        if isChecked == true {
            isChecked = false
        } else {
            isChecked = true
        }
    }
  }
}