在我的待办事项列表应用程序中,我使用静态单元格来显示永不改变的待办事项的NSMutableArray。一旦一个人点击单元格,一个accessoryType of .Checkmark并被标记为已完成。我所要做的和尚未找到答案的是,一旦数组中的每一项都完成并添加了一个复选标记,就会返回整个列表的completionDate。我发现可以在NSArray或NSMutableArray上调用的最接近的方法是-removeAllObjects,但我不希望该数组被破坏。这是一份每日核对表,需要继续提供。我真的很感激有人为我指明了正确的方向,甚至更好的建议,如何做到这一点。谢谢!
var checklistItems: NSMutableArray = []
override func viewDidLoad() {
    super.viewDidLoad()
    loadInitialData()
 func loadInitialData(){
    var item1 = Checklist(name: "Check Tires")
    self.checklistItems.addObject(item1)
    var item2 = Checklist(name: "Check Controls")
    self.checklistItems.addObject(item2)
    var item3 = Checklist(name: "Check Lights")
    self.checklistItems.addObject(item3)
    var item4 = Checklist(name: "Check Oil")
    self.checklistItems.addObject(item4)
    var item5 = Checklist(name: "Check Chassis")
    self.checklistItems.addObject(item5)
    var item6 = Checklist(name: "Check Sidestand")
    self.checklistItems.addObject(item6)
 }
  override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.checklistItems.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("ListPrototypeCell", forIndexPath: indexPath) as UITableViewCell
    // Configure the cell...
    var checklistItems: Checklist = self.checklistItems.objectAtIndex(indexPath.row) as Checklist
    cell.textLabel?.text = checklistItems.itemName
    if checklistItems.completed{
        cell.accessoryType = .Checkmark
    }
    else{
        cell.accessoryType = .None
    }
    return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: false)
        var tappedItem: Checklist = self.checklistItems.objectAtIndex(indexPath.row) as
        Checklist
        tappedItem.completed = !tappedItem.completed
        tableView.reloadData()
}发布于 2015-01-20 21:59:09
我不确定如何从这个列表中返回一个完成日期,可能是通过委托方法或者通过解压缩方法,但是我可以建议一种方法来确定是否检查了整个列表。
如果添加一个NSMutableSet来保存已检查的项,则可以快速确定在选中项集的计数等于数组的计数时检查所有项-
var checklistItems: NSMutableArray = []
var checkedItems = NSMutableSet()
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: false)
        var tappedItem: Checklist = self.checklistItems.objectAtIndex(indexPath.row) as
        Checklist
        tappedItem.completed = !tappedItem.completed
        if (tappedItem.completed) {
           checkedItems.addObject(tappedItem)
        } else {
           checkedItems.removeObject(tappedItem)
        }
        if (self.allChecked()) {
            self.performSegueWithIdentifier("unwindFromChecklist", sender:self)
        } else {
            tableview.reloadData()
        }
}
func allChecked() -> Bool {
    return checkedItems.count == checklistItems.count
}我已经添加了一个展开segue调用--您可以在调用视图控制器中获取展开方法中的当前日期和时间,或者在调用unwind segue之前在该视图控制器的属性中设置当前日期。
发布于 2015-01-20 22:25:00
我认为Paul的方法可以检查您是否真的不需要创建另一个数组。一种“猛击”的方式。
    func allChecked() -> Bool
    {
// create an array with only the uncompleted items 
       let uncompletedArray = self.checkListItems.filter { $0.completed == false}
 // if this array is empty all items are completed
       return uncompletedArray.count == 0;
    }https://stackoverflow.com/questions/28055583
复制相似问题