继承UITableViewDataSource必须实现的方法
有两个方法必须实现,一个是cellForRowAtIndexPath(设定cell内容),还有一个是numberOfRowsInSection(设定行数)。
1、设定行数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 7 }
2、设定cell中内容
//创建单元格(函数名和参数的写法是固定的) func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let row = indexPath.row //定义的是reuseIdentifier: reuserIdentifier中的后一个,是cell的标识 let reuserIdentifier = "Cell" let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: reuserIdentifier) switch row{ case 0: //lineView1是在cell0上方加的一个间隔区 let lineView1=UIView(frame: CGRectMake(0,0,ScreenSize.SCREEN_WIDTH,10)) lineView1.backgroundColor=UIColorFromRGB(244, green: 244, blue: 244, alpha: 1.0) cell.addSubview(lineView1) let str = NSMutableAttributedString(string: "*联系人") //设定cell中文字的颜色 cell.textLabel?.textColor=textColor //str中的首字符的颜色单独设定 str.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, 1)) cell.textLabel?.attributedText = str //在该cell中添加的输入框 let textF = UITextField(frame: CGRectMake(130,12,ScreenSize.SCREEN_WIDTH-70,42)) //为了保存输入的内容 textF.tag=10 textF.delegate = self //文本定义 // textF.text = "emergency" //紧急联络人 let str0 = NSMutableAttributedString(string:"输入联系人") str0.addAttribute(NSForegroundColorAttributeName, value: placeholderColor, range: NSMakeRange(0, str0.length)) //Placeholder占位符定义,占位符在点击输入时,原内容自动消失 textF.attributedPlaceholder = str0 textF.font = UIFont.systemFontOfSize(14) textF.backgroundColor=UIColor.whiteColor() textF.delegate=self cell.addSubview(textF) case 1: let str = NSMutableAttributedString(string: "*Tel") cell.textLabel?.textColor=textColor str.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, 1)) cell.textLabel?.attributedText = str let textF = UITextField(frame: CGRectMake(130,2,ScreenSize.SCREEN_WIDTH-70,42)) textF.tag=11 textF.delegate = self textF.text = "emergencyPhoneNum "//紧急联络人电话 textF.font = UIFont.systemFontOfSize(14) textF.backgroundColor=UIColor.whiteColor() textF.delegate=self textF.keyboardType = UIKeyboardType.PhonePad cell.addSubview(textF) case 2: let str = NSMutableAttributedString(string:"*Planned Date of Departure") cell.textLabel?.textColor=textColor str.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, 1)) cell.textLabel?.attributedText = str if duedate != ""{ cell.detailTextLabel?.text=convertDateToString2(duedate)//拟到期日期 } cell.detailTextLabel!.tag=12 let imgView = UIImageView(frame: CGRectMake(0, 0, 20, 20)) imgView.image = UIImage(named: "date_icon") imgView.contentMode = UIViewContentMode.ScaleAspectFit cell.accessoryView = imgView case 3: let str = NSMutableAttributedString(string:"*Police Subsation") cell.textLabel?.textColor=textColor str.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, 1)) cell.textLabel?.attributedText = str cell.detailTextLabel?.tag=13 cell.detailTextLabel?.text = policeStationList[policeStation]//所属派出所 let imgView = UIImageView(frame: CGRectMake(0, 0, 12, 16)) imgView.image = UIImage(named: "down_icon") imgView.contentMode = UIViewContentMode.ScaleAspectFit cell.accessoryView = imgView case 4: let str = NSMutableAttributedString(string: "*Street") cell.textLabel?.textColor=textColor str.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, 1)) cell.textLabel?.attributedText = str cell.detailTextLabel?.text = "street "//街路巷 cell.detailTextLabel?.tag = 14 let imgView = UIImageView(frame: CGRectMake(0, 0, 12, 12)) imgView.image = UIImage(named: "next_icon") imgView.contentMode = UIViewContentMode.ScaleAspectFit cell.accessoryView = imgView case 5: let str = NSMutableAttributedString(string:"*Detailed Address") cell.textLabel?.textColor=textColor str.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, 1)) cell.textLabel?.attributedText = str let textF = UITextField(frame: CGRectMake(220,2,ScreenSize.SCREEN_WIDTH-70,42)) textF.delegate = self // textF.text = "detailAddress"//详细地址 textF.tag=15 let str0 = NSMutableAttributedString(string:"putDetaiAddr") str0.addAttribute(NSForegroundColorAttributeName, value: placeholderColor, range: NSMakeRange(0, str0.length)) textF.attributedPlaceholder = str0 textF.font = UIFont.systemFontOfSize(14) textF.backgroundColor=UIColor.whiteColor() textF.delegate=self cell.addSubview(textF) case 6: let str = NSMutableAttributedString(string:"*Name of House Owner") cell.textLabel?.textColor=textColor str.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, 1)) cell.textLabel?.attributedText = str let textF = UITextField(frame: CGRectMake(220,2,ScreenSize.SCREEN_WIDTH-70,42)) textF.delegate = self textF.tag = 16 textF.text = "hourseOwnerName"//房主姓名 let str0 = NSMutableAttributedString(string:"putNameHouse") str0.addAttribute(NSForegroundColorAttributeName, value: placeholderColor, range: NSMakeRange(0, str0.length)) textF.attributedPlaceholder = str0 textF.font = UIFont.systemFontOfSize(14) textF.backgroundColor=UIColor.whiteColor() textF.delegate=self textF.keyboardType = UIKeyboardType.PhonePad cell.addSubview(textF) default: "default" }