您的位置:首页 > 移动开发

iOS_Apprentice_2_Checklists学习总结(1)

2014-11-07 22:24 148 查看
正式开始学习Swift之后第一次在博客上写下学习心得,遵照其他前辈的意见。写在这里主要是帮助自己记忆程序的结构和编程流程,因为发现跟着教程走到后来就只剩下看一步做一步,完全没有全局概念,并且迅速的将前面所学的知识忘记,所以要在这里不断的重复逻辑来加强记忆。大不了把所有代码背下来一遍一遍写,总会进步吧...

目前的进度是创建了ChecklistViewController.swift和ChecklistItem.swift两个文件,storyboard中有一个UITableView,制作了5个带Checkmark的项,点击可以勾选和去勾选(Checkmark)。

代码复写:

ChecklistViewController.swift

import UIKit

class ChecklistViewController: UITableViewController {

var Items: [ChecklistItem]

required init (coder aDecoder: NSCoder) {

var items = [ChecklistItem]()

let row0item = ChecklistItem()

row0item.text = "Walk the dog"

row0item.checked = false

super.init(coder aDecoder)

}

override func tableView(tableView: UITableView, numberOfRowSection section: Int) -> Int {

return items.count

}

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

let cell = tableView.dequeueReusableCellWithIdentifier("ChecklistItem") as UITableViewCell

let item = items[indexPath.row]

let label = cell.viewwithTag(1000) as UILabel

label.text = item.text

configureTextForCell(cell, withChecklistItem: item)

configrueCheckmarkForCell(cell, withChecklistItem: item)

return cell

}

override func tableView(tableView: UITableView didSelectRowAtIndexPath indexPath: NSIndexPath) {

if let cell = tableView.cellForRowAtIndexPath(indexPath) {

let item = items[indexPath.row]

item.toggleChecked()

configureCheckmarkForCell(cell, withChecklistItem: item)

}

tableView.deselectRowAtIndexPath(indexPath, animated: true)

}

func configureCheckmarkForCell(cell: UITableViewCell, withChecklistItem item: ChecklistItem) {

if item: checked {

cell.accessoryType = .Checkmark

} else {

cell.accessoryType = .None

}

}

func configureTextForCell(cell: UITableViewCell, withChecklistItem item: ChecklistItem) {

let label = cell.viewWithTag(1000) as UILabel

label.text = item.text

}

}

ChecklistItem.swift

import Foundation
class ChecklistItem {
var text = ""
var checked = false

func togglechecked() {
checked = !checked
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS Swift ChecklistItem