细节页面(实训)
来源:互联网 发布:c语言合法用户标识符 编辑:程序博客网 时间:2024/05/22 16:33
从个人主界面点击,即可进入诸如收藏界面、想去目的地界面等细节界面。分析这些界面,都是利用tabelview视图,且每个tableviewcell的格式基本相同,所以综合几个页面,可以利用同一个基础的自定义的tableview类,在内容不同的界面,只需显示不同的内容即可。
不用storyboard,tableview与collectionview的纯代码实现基本类似。
首先,声明一个UITableView。
var tableview : UITableView!
在细节界面的类继承的类后设置要实现的两个代理,由于没有实现相应的方法,此时会报错,可以忽略这些错误,实现代理方法后错误就会消失。
class DetailViewController: UIViewController,UITableViewDataSource,UITableViewDelegate
接下来,在override func viewDidLoad()中进行初始化。首先初始化tableView,设置其所在的视图的位置以及大小(宽度为获取的屏幕的宽度)。不用storyboard设置cell的identifier,就一定要注册、注册、注册!!!给tableView注册cell,声明这个tableView中的cell使用的是哪个cell类,并设置其identifier。tableView的cell是自定义的,设置了代理和数据源。同时,还可以设置tableView的各种属性,在这里设置了隐藏横向和纵向的滚动条等属性,还可以设置背景颜色、偏移量、cell的预估高度等属性。
super.viewDidLoad() tableview = UITableView(frame: CGRect(x: 0, y: 64, width: screenwidth, height: screenheight-64), style: UITableViewStyle.plain) tableview.delegate = self tableview.dataSource = self tableview.showsHorizontalScrollIndicator = false tableview.showsVerticalScrollIndicator = false tableview.bounces = false tableview.separatorStyle = .none tableview.register(ProductTableViewCell.self, forCellReuseIdentifier: "productcell") self.view.addSubview(tableview)
然后实现代理方法。第一个返回row的行数,有多少个cell栏就返回多少,这里根据以后从服务器接收到的数据决定。第二个代理方法,首先声明一个cell,用identifier将其与之前注册的联系起来,并注意要转换为自定义得到cell类型。通过indexPath的row属性,为cell中的标题和图片赋予接收到的数据中对应的值。在这里并没有用到section分组,但是同样可以使用,section代表有几个节点,row代表每个节点有几行,如果未设置,则默认为一行。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return tits.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { var cell = tableview.dequeueReusableCell(withIdentifier: "productcell") as? ProductTableViewCell if cell == nil{ cell = UITableViewCell(style:UITableViewCellStyle.default , reuseIdentifier: "productcell") as? ProductTableViewCell } var imageString = images[indexPath.row] var titleString = tits[indexPath.row] var priceString = prices[indexPath.row] cell?.setupUI(imageString: imageString, titleString: titleString, priceString: priceString) return cell! }
至此,tableview基本已经实现,接下来看一下自定义的cell。自定义的cell继承UITableViewCell,在其中声明cell中要包含的诸如图片、标签框等控件。重载init方法,初始化各类控件,同时可以设置cell的一些属性,诸如高亮等效果。注意一定、一定、一定不要忘记将初始化的空间利用addview方法加入到cell中,否则无法展示。值得一提的是在这里自定义了一个setupUI方法,以实现每次从外部传入参数,在cell中可以展现出来。最后还要重载setSelected方法,设置在点击cell时,会触发什么事件。
override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) // 设置选中cell时无高亮等效果 self.selectionStyle = .none imageview = UIImageView(frame: CGRect(x: 16, y: 10, width: 105, height: 80)) titleLabel = UILabel(frame: CGRect(x: 136, y: 20, width: 80, height: 20)) priceLabel = UILabel(frame: CGRect(x: 136, y: 25, width: 160, height: 40)) titleLabel?.textAlignment = .center titleLabel?.font = UIFont.systemFont(ofSize: 17) priceLabel?.textAlignment = .center priceLabel?.font = UIFont.systemFont(ofSize: 12) titleLabel?.textColor = kRGBColorFromHex(rgbValue: 0x282828) priceLabel?.textColor = kRGBColorFromHex(rgbValue: 0x656565) self.contentView.addSubview(imageview!) self.contentView.addSubview(titleLabel!) self.contentView.addSubview(priceLabel!) } required init?(coder aDecoder: NSCoder) { //fatalError("init(coder:) has not been implemented") super.init(coder: aDecoder)! } func setupUI(imageString:String,titleString:String,priceString:String){ self.imageview?.image=UIImage(named: imageString) self.titleLabel?.text=titleString self.priceLabel?.text=priceString } 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 }
- 细节页面(实训)
- 页面细节处理心得
- JSP页面细节
- ASP.NET页面处理细节
- 页面缓存的小细节
- Jsp页面中的注释细节
- 【SEO与页面设置细节】
- 影响SEO的页面制作细节
- 页面内容以及细节怎么优化
- 关注细节之页面传值
- asp.net如何加快页面加载(三)——后台代码之细节2(while,do..while,for,foreach)
- 小小细节(一)
- 平时细节(php)
- Java细节(9.22)
- Matlab细节(1)
- Matlab细节(2)
- Matlab细节(3)
- 各种细节(RP++)
- mysql初识
- Java基础-多线程的中常用方法
- 长度单位em rem
- CString 和 LPCTSTR 之间的转换 及 LPSTR、LPWSTR、LPCSTR、LPCWSTR、LPTSTR、LPCTSTR的区分与转化
- jar与dex互转工具,apk源码查看工具
- 细节页面(实训)
- springmvc多文件上传
- 线性表
- CentOS开机异常修复汇总
- IntelliJ IDEA 将 Maven 构建的 Java 项目打包
- AJAX之二级联动下拉列表
- 《Unbroken》——Everything is possible
- C++实验6:数组操作
- kali linux2.0安装中文输入法googlepinyin