写在前面的话:
有时候就是一个界面已经很乱了,但是必须要重构一些东西进去的时候,你不方便再使用别的控件去堆砌界面时,只好用已有的写才能更加方便后续的操作,比如下面是在一个已经使用了tableview的情况下再在里面插入一个这样的段
下面是cell的代码,布局以及模型赋值
import UIKit
class jingxuanTableViewCell: UITableViewCell {
// 左边视图
var btnLeft = UIButton()
var imgLeft = UIImageView()
var desLeft = UILabel()
var priLeft = UILabel()
var numLeft = UILabel()
var lineMiddle = UIImageView()
var lineLeftBottom = UIImageView()
// 右边视图
var btnRight = UIButton()
var imgRight = UIImageView()
var desRight = UILabel()
var priRight = UILabel()
var numRight = UILabel()
var lineRightBottom = UIImageView()
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.createTableViewCellUI()
}
func createTableViewCellUI(){
let view = self.contentView
// MARK: - 左侧视图
view.addSubview(btnLeft)
view.backgroundColor = UIColor.rgb(244, 248, 251)
btnLeft.snp.makeConstraints { (make) in
make.left.equalTo(view).offset(0)
make.width.equalTo(screenWidth/2-5)
make.height.equalTo(view.height-10)
make.top.bottom.equalTo(view)
}
btnLeft.backgroundColor = UIColor.white
// 商品图片
btnLeft.addSubview(imgLeft)
imgLeft.snp.makeConstraints { (make) in
make.left.equalTo(btnLeft.snp.left).offset(0)
make.width.equalTo(btnLeft.snp.width)
make.height.equalTo(screenHeight*0.4*0.7)
make.top.equalTo(btnLeft)
}
// 商品描述
btnLeft.addSubview(desLeft)
desLeft.snp.makeConstraints { (make) in
make.left.equalTo(imgLeft.snp.left).offset(10)
make.width.equalTo(screenWidth/2-25)
make.top.equalTo(imgLeft.snp.bottom).offset(10)
make.height.equalTo(screenHeight*0.4*0.15)
}
desLeft.numberOfLines = 0
desLeft.font = UIFont.systemFont(ofSize: 13)
desLeft.textColor = UIColor.color333()
// 商品价格
btnLeft.addSubview(priLeft)
priLeft.snp.makeConstraints { (make) in
make.left.equalTo(btnLeft.snp.left).offset(10)
make.width.equalTo(screenWidth*0.3)
make.top.equalTo(desLeft.snp.bottom).offset(5)
make.height.equalTo(screenHeight*0.4*0.1)
}
priLeft.font = UIFont.systemFont(ofSize: 15)
// 商品已购买人数
btnLeft.addSubview(numLeft)
numLeft.snp.makeConstraints { (make) in
make.right.equalTo(btnLeft.snp.right).offset(-10)
make.width.equalTo(screenWidth*0.25)
make.centerY.equalTo(priLeft.snp.centerY)
make.height.equalTo(screenHeight*0.4*0.1)
}
numLeft.textAlignment = .right
numLeft.font = UIFont.systemFont(ofSize: 10)
// 中间线
view.addSubview(lineMiddle)
lineMiddle.snp.makeConstraints { (make) in
make.left.equalTo(btnLeft.snp.right).offset(0)
make.height.equalTo(btnLeft.snp.height)
make.width.equalTo(10)
}
lineMiddle.backgroundColor = UIColor.rgb(244, 248, 251)
// 下方线
view.addSubview(lineLeftBottom)
lineLeftBottom.snp.makeConstraints { (make) in
make.left.equalTo(view).offset(0)
make.height.equalTo(10)
make.width.equalTo(screenWidth/2)
make.top.equalTo(btnLeft.snp.bottom).offset(0)
}
lineLeftBottom.backgroundColor = UIColor.rgb(244, 248, 251)
// MARK: - 右侧视图
view.addSubview(btnRight)
btnRight.backgroundColor = UIColor.white
btnRight.snp.makeConstraints { (make) in
make.right.equalTo(view).offset(0)
make.width.equalTo(screenWidth/2-5)
make.height.equalTo(view.height-10)
make.top.bottom.equalTo(view)
}
// 商品图片
btnRight.addSubview(imgRight)
imgRight.snp.makeConstraints { (make) in
make.left.equalTo(btnRight.snp.left).offset(0)
make.width.equalTo(btnRight.snp.width)
make.height.equalTo(screenHeight*0.4*0.7)
make.top.equalTo(btnRight)
}
// 商品描述
btnRight.addSubview(desRight)
desRight.snp.makeConstraints { (make) in
make.left.equalTo(imgRight.snp.left).offset(10)
make.width.equalTo(screenWidth/2-25)
make.top.equalTo(imgRight.snp.bottom).offset(10)
make.height.equalTo(screenHeight*0.4*0.15)
}
desRight.numberOfLines = 0
desRight.font = UIFont.systemFont(ofSize: 13)
desRight.textColor = UIColor.color333()
// 商品价格
btnRight.addSubview(priRight)
priRight.snp.makeConstraints { (make) in
make.left.equalTo(btnRight.snp.left).offset(10)
make.width.equalTo(screenWidth*0.3)
make.top.equalTo(desRight.snp.bottom).offset(5)
make.height.equalTo(screenHeight*0.4*0.1)
}
priRight.font = UIFont.systemFont(ofSize: 15)
// 商品已购买人数
btnRight.addSubview(numRight)
numRight.snp.makeConstraints { (make) in
make.right.equalTo(btnRight.snp.right).offset(-10)
make.width.equalTo(screenWidth*0.25)
make.centerY.equalTo(priRight.snp.centerY)
make.height.equalTo(screenHeight*0.4*0.1)
}
numRight.textAlignment = .right
numRight.font = UIFont.systemFont(ofSize: 10)
// 下方线
view.addSubview(lineRightBottom)
lineRightBottom.snp.makeConstraints { (make) in
make.right.equalTo(view.snp.right).offset(0)
make.height.equalTo(10)
make.width.equalTo(screenWidth/2)
make.top.equalTo(btnRight.snp.bottom).offset(0)
}
lineRightBottom.backgroundColor = UIColor.rgb(244, 248, 251)
}
// 刷新左边的视图
func refreshLeftUI(goodsmodel:CommodityDetailsGMapModel){
self.imgLeft.sd_setImage(with: URL(string: (String(format: "%@/loadImg.htm?path=%@", url_host,goodsmodel.logoUrl ?? ""))), placeholderImage: UIImage.init(named: "list_default"), options: SDWebImageOptions.refreshCached)
self.desLeft.text = goodsmodel.goodsName
var price:String?
// let intP = goodsmodel.price
if Double(goodsmodel.price!) >= 100000.0{
price = String(format:"¥%.2f万元",((goodsmodel.price ?? 0) / 10000))
}else{
price = String(format:"¥\(goodsmodel.price ?? 0)")
}
self.priLeft.text = price
self.numLeft.text = String(format: "\(goodsmodel.saleSum ?? 0)人已买")
}
// 刷新右边的视图
func refreshRightUI(goodsmodel:CommodityDetailsGMapModel){
self.imgRight.sd_setImage(with: URL(string: (String(format: "%@/loadImg.htm?path=%@", url_host,goodsmodel.logoUrl ?? ""))), placeholderImage: UIImage.init(named: "list_default"),options: SDWebImageOptions.refreshCached)
self.desRight.text = goodsmodel.goodsName
var price:String?
// let intP = goodsmodel.price
if Double(goodsmodel.price!) >= 100000.0{
price = String(format:"¥%.2f万",((goodsmodel.price ?? 0) / 10000))
}else{
price = String(format:"¥\(goodsmodel.price ?? 0)")
}
self.priRight.text = price
self.numRight.text = String(format: "\(goodsmodel.saleSum ?? 0)人已买")
}
}
下面是在控制器里的使用
// 根据个数判断显示的行数 因为在此处相当于是一行显示两个数据源
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (self.shareArr.count == 1) {
return self.shareArr.count+1;
}
let result = self.shareArr.count%2;
if (result == 0) {
return self.shareArr.count/2;
}else if (result == 1){
return self.shareArr.count/2+1;
}
return 0
}
// 这里是很重要的,一定要考虑到总个数%2之后的余数 然后才好赋值
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "jingxuanTableViewCell") as! jingxuanTableViewCell
cell.btnLeft.tag = 2*indexPath.row
cell.btnRight.tag = 2*indexPath.row+1
cell.btnLeft.addTarget(self, action: #selector(cellButtonForclick(btn:)), for: .touchUpInside)
cell.btnRight.addTarget(self, action: #selector(cellButtonForclick(btn:)), for: .touchUpInside)
if self.shareArr.count == 1{
let model = self.shareArr[0]
cell.refreshStoreLeftUI(goodsmodel: model as! StoreDetailGoodslistMapsModel)
}
if (self.shareArr.count) > 1{
let result = (self.shareArr.count) % 2
if result == 0{
// 行的索引*2
let model1 = self.shareArr[indexPath.row*2]
cell.refreshStoreLeftUI(goodsmodel: model1 as! StoreDetailGoodslistMapsModel)
// 行的索引*2+1
let model2 = self.shareArr[indexPath.row*2+1]
cell.refreshStoreRightUI(goodsmodel: model2 as! StoreDetailGoodslistMapsModel)
//
}
if result == 1{
let model1 = self.shareArr[indexPath.row*2]
cell.refreshStoreLeftUI(goodsmodel: model1 as! StoreDetailGoodslistMapsModel)
cell.btnRight.backgroundColor = UIColor.white
if indexPath.row*2+1 <= (self.shareArr.count)-1 {
let model2 = self.shareArr[indexPath.row*2+1] as! StoreDetailGoodslistMapsModel
cell.refreshStoreRightUI(goodsmodel: model2)
}else if indexPath.row*2+1 == self.shareArr.count {
cell.backgroundColor = UIColor.rgb(244, 248, 251)
cell.imgRight.sd_setImage(with: URL(string: (String(format:""))), placeholderImage: UIImage.init(named: ""), options: SDWebImageOptions.refreshCached)
cell.desRight.text = ""
cell.priRight.text = ""
cell.numRight.text = ""
cell.btnRight.backgroundColor = UIColor.clear
}
}
}
return cell
}
基本上就是这些了,这个用起来的时候还是很方便的,但是一定要注意数据源那里的判断,否则极易引起数组越界等问题。