【背景】
折腾:
期间,继续去在新添加的页面中,添加列表table的视图:tableview。
【折腾过程】
1.参考已有的别人代码。
2.搜:
swift create table view
参考:
How to make a simple tableview with iOS 8 and Swift
Start Developing iOS Apps (Swift): Create a Table View
Swift 2 Tutorial Part 3: Tuples, Protocols, Delegates, and Table Views – Ray Wenderlich
【总结】
后来是参考别人的代码后,经过整理,可以生成table和cell了:
1.首先是当前类,要继承UITableViewDelegate, UITableViewDataSource
2.再去实现对应的四个函数:
- numberOfRowsInSection:有几行
- heightForRowAtIndexPath:分别返回每行的高度
- cellForRowAtIndexPath:分别返回每一行的cell,包含了具体的内容的设置,比如label,image,右边是打对勾还是右箭头,等等
- didSelectRowAtIndexPath:当选中了某行,如何处理。比如是跳转到新的子页面。
相关代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | import UIKit class MyWalletViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { var itemNameList = [ "item1" , "item2" , "item3" , "item4" ] var tableView:UITableView? init() { super.init(nibName: nil, bundle: nil) } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override func viewDidLoad() { super.viewDidLoad() let backImage = UIImage(named: "back.png" ) let backBtn = UIButton() backBtn.frame = CGRectMake(10, 30, 24, 24) backBtn.addTarget(self, action:Selector( "backToPrevWindow:" ), forControlEvents:UIControlEvents.TouchUpInside) backBtn.setImage(backImage, forState:UIControlState.Normal) backBtn.setImage(backImage, forState: UIControlState.Selected) self.view.addSubview(backBtn) let titleLabel = UILabel() titleLabel.frame = CGRectMake(40, 0, self.view.bounds.width - 80, 44) titleLabel.text = "我的钱包" titleLabel.textColor = UIColor.whiteColor() titleLabel.font = UIFont.boldSystemFontOfSize(18) titleLabel.textAlignment = NSTextAlignment.Center tabbgView.addSubview(titleLabel) //创建表格视图 print(self.itemNameList) self.tableView = UITableView(frame: CGRectMake(0, 70, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))) self.tableView!.delegate = self self.tableView!.dataSource = self self.tableView?.backgroundColor = UIColor(hexString: "#e6e6e6" ) self.tableView?.separatorColor = UIColor(hexString: "#dbdbdb" ) self.tableView?.separatorInset = UIEdgeInsetsMake(0, 10, 0, 0) //创建一个重用的单元格 self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell" ) self.view.addSubview(tableView!) } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.itemNameList.count } func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return 55 } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { print( "cellForRowAtIndexPath indexPath = \(indexPath)" ) let indentify:String = "SwiftCell" let cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: indentify) cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator cell.backgroundColor = UIColor.whiteColor() cell.textLabel?.text = self.itemNameList[indexPath.row] cell.textLabel?.textColor = UIColor(hexString: "#4d4d4d" ) cell.textLabel?.font = UIFont.boldSystemFontOfSize(15) cell.detailTextLabel?.text = "0.0元" cell.detailTextLabel?.textColor = UIColor.redColor(); cell.detailTextLabel?.font = UIFont.boldSystemFontOfSize(13) cell.detailTextLabel?.textAlignment = NSTextAlignment.Right return cell } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { self.tableView!.deselectRowAtIndexPath(indexPath, animated: true ) self.tableView!.cellForRowAtIndexPath(indexPath)!.selected = false switch indexPath.row { case 0: break case 1: break ; case 2: break ; case 3: break ; case 4: break ; default : break ; } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func backToPrevWindow(button : UIButton) { self.navigationController?.popViewControllerAnimated( true ) } } |
供参考。
转载请注明:在路上 » 【已解决】Swift创建列表视图TableView