下面是一个静态的table view
于图可知有两个section头是11..和22..,其中222...是一个table view cell !并且从图可知道样式是left Detail,Accessory(选的是Discloure Indicator)这两个分别图中的Title detail 和 右边的>
下面是一个动态的table view
写一个viewcontroll视图控制器
import UIKitclass viewcontroll :UITableViewController{ var person:[Person]! override func viewDidLoad() { super.viewDidLoad() person = PersonImpl().generatePerson() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } //返回行数 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return person.count } // 给每行赋值 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("demo")! let p = person[indexPath.row] let nameLabel = cell.viewWithTag(1) as! UILabel nameLabel.text = p.name let ageLabel = cell.viewWithTag(2) as! UILabel ageLabel.text = "\(p.age)" return cell }}
而赋值有3种方法
//第一种:直接自己创建cell // let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cellDemo")// cell.textLabel?.text = "aaa"// return cell //第二种:演示IndexPath参数的使用// let cell = UITableViewCell(style: .Default, reuseIdentifier: "cellDemo")// if indexPath.row == 0 {// //let cell = UITableViewCell(style: .Default, reuseIdentifier: "cellDemo")// cell.textLabel?.text = "aaa"// //return cell// }else if indexPath.row == 1 {// //let cell = UITableViewCell(style: .Default, reuseIdentifier: "cellDemo")// cell.textLabel?.text = "bbbb"// //return cell// } else if indexPath.row == 2 {// //let cell = UITableViewCell(style: .Default, reuseIdentifier: "cellDemo")// cell.textLabel?.text = "ccc"// // return cell// }// return cell //第三种: let cell = tableView.dequeueReusableCellWithIdentifier("cellDemo") cell?.textLabel?.text = "aaaa" return cell!
再写一个person类
import UIKit//模型类class Person { var name: String var age: Int init(name: String,age: Int){ self.name = name self.age = age } }// PersonBLLclass PersonImpl { func generatePerson() -> [Person] { //persondao. //persondal.() var result = [Person]() let p = Person(name: "david1", age: 11) result.append(p) let p2 = Person(name: "david2", age: 22) result.append(p2) let p3 = Person(name: "david3", age: 33) result.append(p3) return result }}
把表格行identifiter改为cell