定义值类型 AppsModel文件
import UIKit
class AppsModel: NSObject {
var imageName:String!
var appName:String!
var appDescription:String!
init(imageName image_Name:String,app_Name :String,app_description :String) {
self.imageName = image_Name;
self.appName = app_Name;
self.appDescription = app_description;
}
}
自定义 单元格的布局 MyTableViewCell文件
import UIKit
class MyTableViewCell: UITableViewCell {
var iconImageView:UIImageView!
var appNameLabel:UILabel!
var decLabel:UILabel!
func showAppInfoWithModel(model:AppsModel)
{
self.iconImageView.image = UIImage(named:model.imageName);
self.appNameLabel.text = model.appName;
self.decLabel.text = model.appDescription;
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier);
//创建imageView
self.iconImageView = UIImageView(frame: CGRectMake(10, 5, 40, 40));
//self.iconImageView.image = UIImage(named:"Image");
self.addSubview(self.iconImageView);
//创建appNameLabel
self.appNameLabel = UILabel(frame: CGRectMake(60, 0, 220, 15));
//self.appNameLabel.text = "123213";
appNameLabel.font = UIFont.systemFontOfSize(16);
self.addSubview(self.appNameLabel);
//创建decLabel
self.decLabel = UILabel(frame: CGRectMake(60, 15, 220, 35));
self.decLabel.font = UIFont.systemFontOfSize(12);
//self.decLabel.text = "346"
self.addSubview(self.decLabel);
}
required init(coder aDecoder: NSCoder) {
fatalError("init(code:)has not brrn implomented");
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
3 实现UITableView的文件
有一个数组
var items:[AppModel]!
var model1:AppsModel = AppsModel(imageName: "Image", app_Name: "Football Maze", app_description: "足球迷宫,迷宫的新玩法,益智虚拟迷宫游戏。快来挑战你的空间想象,足球迷宫带你到一个不同的世界… 迷宫大家都在玩,你还在等什么。")
var model2:AppsModel = AppsModel(imageName: "Image", app_Name: "租房点评", app_description: "租房被骗?现在开始,你来改变这一切!《租房点评》为你而备,租房无忧!")
var model3:AppsModel = AppsModel(imageName: "Image", app_Name: "iJump", app_description: "摇动手机,松鼠就可以运动啦,越跳越高,注意会有虫子咬坏跳板哦,祝你玩得开心")
var model4:AppsModel = AppsModel(imageName: "Image", app_Name: "哪里逃", app_description: "哪里逃 是一款躲避类游戏,拖动美女图片,躲避,追来的帅锅,帅锅人数越来越多,不要被追到哦。")
//修改数组值
items = [model1,model2,model3,model4]
实现代理方法
// 数据源 每一行的内容
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIndentifier :String = "cellIdentifier";
var cell:MyTableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIndentifier) as? MyTableViewCell
if cell == nil
{
cell = MyTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellIndentifier)
//cellModel = MyTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellIndentifier);
cell?.selectionStyle = .Gray;
//设置选中cell样式
cell!.selectionStyle = .Gray;
//设置cell后面箭头样式
cell!.accessoryType = .DisclosureIndicator;
}
var cellModel:AppsModel = self.items[indexPath.row]
//通过自定义方法给cell赋值
cell?.showAppInfoWithModel(cellModel)
return cell! ;
}