UItableView自定义&封装
一:Model
1 class AppsModel: NSObject { 2 3 //定义模型的三个属性 4 var imageName:String! //图片名称 5 var appName:String! //应用名称 6 var appDescription:String! //应用描述 7 8 9 10 //自定义初始化方法 11 init(imageName image_Name:String , app_Name:String , app_Description:String) { 12 self.imageName=image_Name 13 self.appName=app_Name 14 self.appDescription=app_Description 15 } 16 17 18 19 // MARK: - NSCoding 20 func encodeWithCoder(_encoder: NSCoder) 21 { 22 _encoder.encodeObject(self.imageName, forKey: "M_imageName") 23 _encoder.encodeObject(self.appName, forKey: "M_appName") 24 _encoder.encodeObject(self.appDescription, forKey: "M_appDescription") 25 } 26 27 28 init(coder decoder: NSCoder) 29 { 30 // imageName = decoder.decodeObjectForKey("M_imageName") as String 31 // appName = decoder.decodeObjectForKey("M_appName") as String 32 // appDescription = decoder.decodeObjectForKey("M_appDescription") as String 33 34 //2015年5月2号修改 35 imageName = decoder.decodeObjectForKey("M_imageName") as! String 36 appName = decoder.decodeObjectForKey("M_appName") as! String 37 appDescription = decoder.decodeObjectForKey("M_appDescription") as! String 38 } 39 40 }
二:View
1 class MyTableViewCell: UITableViewCell { 2 3 var iconImageView:UIImageView! //图片 4 var appNameLabel:UILabel! //标题 5 var decLabel:UILabel! //描述 6 7 //赋值方法 - 显示cell内容方法 8 func showAppInfoWithModel(model:AppsModel) 9 { 10 //获取model中得图片 11 iconImageView.image = UIImage(named: model.imageName) 12 13 //获取model中app名称 14 appNameLabel.text = model.appName 15 16 //获取model中app描述 17 decLabel.text = model.appDescription 18 } 19 20 override init(style: UITableViewCellStyle, reuseIdentifier: String?) 21 { 22 super.init(style: style, reuseIdentifier: reuseIdentifier) 23 24 //创建iconImageView 25 iconImageView = UIImageView(frame: CGRectMake(10, 5, 40, 40)) 26 self.addSubview(iconImageView) 27 28 //创建appNameLabel 29 appNameLabel = UILabel(frame: CGRectMake(60, 0, 220, 15)) 30 appNameLabel.font = UIFont.systemFontOfSize(16) 31 self.addSubview(appNameLabel) 32 33 //创建decLabel 34 decLabel = UILabel(frame: CGRectMake(60, 15, 220, 35)) 35 decLabel.font = UIFont.systemFontOfSize(12) 36 decLabel.numberOfLines = 2 37 decLabel.textColor = UIColor.lightGrayColor() 38 self.addSubview(decLabel) 39 40 } 41 42 required init(coder aDecoder: NSCoder) { 43 fatalError("init(coder:) has not been implemented") 44 } 45 46 47 48 override func awakeFromNib() { 49 super.awakeFromNib() 50 // Initialization code 51 } 52 53 override func setSelected(selected: Bool, animated: Bool) { 54 super.setSelected(selected, animated: animated) 55 56 // Configure the view for the selected state 57 } 58 59 }
三:Controller
1 class UITableViewControllerCustom: UIViewController, UITableViewDataSource, UITableViewDelegate { 2 3 var titleString:String! 4 5 @IBOutlet var titleLabel:UILabel! 6 @IBOutlet var listTableView : UITableView! 7 8 9 //定义数组 10 var items:[AppsModel]! 11 12 13 //返回按钮事件 14 @IBAction func backButtonClick() 15 { 16 self.navigationController?.popViewControllerAnimated(true) 17 } 18 19 20 override func viewDidLoad() { 21 super.viewDidLoad() 22 23 titleLabel.text = titleString 24 25 //定义三个模型对象 26 var model1:AppsModel = AppsModel(imageName: "appIcon1.png", app_Name: "Football Maze", app_Description: "足球迷宫,迷宫的新玩法,益智虚拟迷宫游戏。快来挑战你的空间想象,足球迷宫带你到一个不同的世界… 迷宫大家都在玩,你还在等什么。") 27 var model2:AppsModel = AppsModel(imageName: "appIcon2.png", app_Name: "租房点评", app_Description: "租房被骗?现在开始,你来改变这一切!《租房点评》为你而备,租房无忧!") 28 var model3:AppsModel = AppsModel(imageName: "appIcon3.png", app_Name: "iJump", app_Description: "摇动手机,松鼠就可以运动啦,越跳越高,注意会有虫子咬坏跳板哦,祝你玩得开心") 29 var model4:AppsModel = AppsModel(imageName: "appIcon4.png", app_Name: "哪里逃", app_Description: "哪里逃 是一款躲避类游戏,拖动美女图片,躲避,追来的帅锅,帅锅人数越来越多,不要被追到哦。") 30 31 //修改数组值 32 items = [model1,model2,model3,model4] 33 34 //TabelView刷新 35 listTableView.reloadData() 36 37 // Do any additional setup after loading the view. 38 } 39 40 override func didReceiveMemoryWarning() { 41 super.didReceiveMemoryWarning() 42 // Dispose of any resources that can be recreated. 43 } 44 45 46 /* 47 // MARK: - Navigation 48 49 // In a storyboard-based application, you will often want to do a little preparation before navigation 50 override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { 51 // Get the new view controller using segue.destinationViewController. 52 // Pass the selected object to the new view controller. 53 } 54 */ 55 56 57 //MARK: - UITableViewDelegate 58 //tableView数据源:返回行数 59 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 60 { 61 return items.count 62 } 63 64 //tableView 数据源:每一行高度 65 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 66 { 67 return 50 68 } 69 70 //tableView数据源:每一行内容 71 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 72 { 73 //Cell标示符,代表一系列 74 // OC:使用static, swift:使用let 75 let cellIdentifier: String = "cellIdentifier" 76 77 //通过cellIdentifier标示符取没有使用的Cell 78 //有可能不存在,所以使用:optional 79 var cell: MyTableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? MyTableViewCell 80 81 //如果cell取到是空 82 if cell == nil { // no value 83 84 //创建新的MyTableViewCell实例 85 //cell样式:UITableViewCellStyle.Default 86 //cell标示符:cellIdentifier 87 cell = MyTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellIdentifier) 88 89 //设置字体 90 // cell!.textLabel.font = UIFont.systemFontOfSize(14) 91 //2015年4月10号修改 92 cell!.textLabel?.font = UIFont.systemFontOfSize(14) 93 94 95 //设置选中cell样式 96 cell!.selectionStyle = .Gray; 97 98 //设置cell后面箭头样式 99 cell!.accessoryType = .DisclosureIndicator; 100 } 101 102 var cellModel:AppsModel = self.items[indexPath.row] 103 104 //通过自定义方法给cell赋值 105 cell?.showAppInfoWithModel(cellModel) 106 107 return cell!; 108 } 109 110 //tableView代理:点击一行 111 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 112 { 113 //释放选中效果 114 tableView.deselectRowAtIndexPath(indexPath, animated: true) 115 116 var urlString:String! = "https://itunes.apple.com/us/app/fruit-storm/id859713088?l=zh&ls=1&mt=8" 117 118 if indexPath.row == 0 119 { 120 urlString = "https://itunes.apple.com/us/app/football-maze/id879720177?l=zh&ls=1&mt=8" 121 }else if indexPath.row == 1 122 { 123 urlString = "https://itunes.apple.com/us/app/zu-fang-dian-ping/id893902071?l=zh&ls=1&mt=8" 124 }else if indexPath.row == 2 125 { 126 urlString = "https://itunes.apple.com/us/app/ijump/id877475648?l=zh&ls=1&mt=8" 127 }else if indexPath.row == 3 128 { 129 urlString = "https://itunes.apple.com/us/app/na-li-tao/id880016522?l=zh&ls=1&mt=8" 130 } 131 132 UIApplication.sharedApplication().openURL(NSURL(string: urlString)!) 133 } 134 }
时间: 2024-10-09 15:28:40