细节页面(实训)

来源:互联网 发布: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    }



0 0
原创粉丝点击