您的位置:首页 > 移动开发 > IOS开发

Together项目IOS平台开发07

2017-06-01 10:30 344 查看
这次我在上次做的推荐界面的基础上略微做了一些修改,并且添加了一些内容,来使得整个项目较为完整。在中期的设计上,我并没有写专门的方法来添加项目,而是通过一个switch语句来填充项目。再就是调整了tableviewcell的一个高度。这里每个cell中的图片我都用了相同大小,项目使用的是京东众筹的内容进行填充。



import UIKit

class RecommendViewController: UIViewController,SliderGalleryControllerDelegate,UITableViewDelegate,UITableViewDataSource {

let screenSizeWidth = UIScreen.main.bounds.size.width

let screenSizeHeight = UIScreen.main.bounds.size.height

//图片轮播组件
var sliderGallery : SliderGalleryController!

var tableview0 : UITableView?

var dataArray:[String]!

override func viewDidLoad() {

super.viewDidLoad()

dataArray = [String]()
for i in 1 ..< 10
{
dataArray.append("第\(i)行")
}

self.view.backgroundColor = UIColor.white

tableview0 = UITableView(frame:CGRect(x:0,y:0,width:screenSizeWidth,height:screenSizeHeight),style:.plain)

self.view.addSubview(tableview0!)

// tableview0?.rowHeight = 70

tableview0?.dataSource = self

tableview0?.delegate = self

//初始化图片轮播组件
sliderGallery = SliderGalleryController()

sliderGallery.delegate = self

sliderGallery.view.frame = CGRect(x: 0, y: 70, width: screenSizeWidth,
height: (screenSizeWidth-100)/4*3);

//将图片轮播组件添加到当前视图
self.addChildViewController(sliderGallery)

tableview0?.tableHeaderView = sliderGallery.view
//添加组件的点击事件
let tap = UITapGestureRecognizer(target: self,action: #selector(RecommendViewController.handleTapAction(_:)))

sliderGallery.view.addGestureRecognizer(tap)

}
//图片轮播组件协议方法:获取内部scrollView尺寸
func galleryScrollerViewSize() -> CGSize {
return CGSize(width: screenSizeWidth, height:(screenSizeWidth-100)/4*3)
}

//图片轮播组件协议方法:获取数据集合
func galleryDataSource() -> [String] {
return ["https://img30.360buyimg.com/cf/jfs/t5629/79/1723287874/42778/cd1e88e3/59291fabNd8dcac86.jpg",
"https://img30.360buyimg.com/cf/jfs/t5209/197/2443136947/49313/d0126d01/591ac9d1N2eea88bd.jpg",
"https://img30.360buyimg.com/cf/jfs/t5311/292/2421593131/548229/a5da55a5/591aa5daN34ee9c6c.jpg"]
}

//点击事件响应
func handleTapAction(_ tap:UITapGestureRecognizer)->Void{
//获取图片索引值
let index = sliderGallery.currentIndex

//弹出索引信息
let alertController = UIAlertController(title: "您点击的图片索引是:",message: "\(index)", preferredStyle: .alert)

let cancelAction = UIAlertAction(title: "确定", style: .cancel, handler: nil)

alertController.addAction(cancelAction)

self.present(alertController, animated: true, completion: nil)
}

// //设置每一行的内容
// func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//
// let identify:String = "SwiftCell"
//
// var cell = tableView.dequeueReusableCell(withIdentifier: identify, for: indexPath as IndexPath)
//
// cell.textLabel?.text=dataArray![indexPath.row]
// return cell
// }
//
// //设置需要展示的行数
// func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// return dataArray.count
// }

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return dataArray.count
}
func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

return 70
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

var cell = UITableViewCell()

cell.textLabel?.text=dataArray![indexPath.row]

cell.textLabel?.font = UIFont.systemFont(ofSize: 15)

cell.textLabel?.numberOfLines = 4

switch indexPath.row {

case 0:

let url0 = URL(string: "https://img30.360buyimg.com/cf/jfs/t6085/255/17447856/37963/aebe82c7/5925110fN74a29f37.jpg")
//从网络获取数据流
let data0 = try! Data(contentsOf: url0!)
//通过数据流初始化图片
let newImage0 = UIImage(data: data0)

cell.imageView?.image = newImage0

cell.textLabel?.text = "极智派智能手环\n极智派品牌以倡导科技创新为生活提升品质与便利为使命。极智派专注于智能硬件创新、制造、应用,企业拥有一流的设计、开发、生产经验,凭借创始人深耕智能制造业12年的行业沉淀,极智派品牌将一如既往的为智能爱好人士提供最优质的智能硬件产品!"

case 1:

let url1 = URL(string: "https://img30.360buyimg.com/cf/jfs/t5614/290/1161600191/77006/5cafbd9c/5923f0f8N6bcb4e5b.jpg")
//从网络获取数据流
let data1 = try! Data(contentsOf: url1!)
//通过数据流初始化图片
let newImage1 = UIImage(data: data1)

cell.imageView?.image = newImage1

cell.textLabel?.text = "4岁用到10岁的教育机器人。"

case 2:

let url2 = URL(string: "https://img30.360buyimg.com/cf/jfs/t5167/24/2587135137/94049/5832b93a/591c1a6bN005fa6a8.gif")
//从网络获取数据流
let data2 = try! Data(contentsOf: url2!)
//通过数据流初始化图片
let newImage2 = UIImage(data: data2)

cell.imageView?.image = newImage2

cell.textLabel?.text = "守住先辈的技艺,中国古刀剑\n嵩山宝剑厂,建厂于1983年,我们坚持沿袭古法工艺铸剑,现任厂长曹延朝为第十一代传人"

case 3:

let url3 = URL(string: "https://img30.360buyimg.com/cf/jfs/t5977/8/435131259/61364/57c5b0c7/5927f9b9N8b965dec.gif")
//从网络获取数据流
let data3 = try! Data(contentsOf: url3!)
//通过数据流初始化图片
let newImage3 = UIImage(data: data3)

cell.imageView?.image = newImage3

cell.textLabel?.text = "ENS智能双系统安防门锁\n南昌和旭智能科技有限公司是一家以安防监控、智能家居、指纹锁等产品智能化建设及运营为一体的高科技企业。"
case 4:

let url4 = URL(string: "https://img30.360buyimg.com/cf/jfs/t5749/188/101054691/73590/b0424a05/591d4749N579f080e.png")
//从网络获取数据流
let data4 = try! Data(contentsOf: url4!)
//通过数据流初始化图片
let newImage4 = UIImage(data: data4)

cell.imageView?.image = newImage4

cell.textLabel?.text = "Airknife陶瓷主厨刀\n以创新科技与前沿设计改变生活,Airknife正是陶瓷刀进入专业厨刀领域里程碑的一步"

default:

let image3 = UIImage(named:"IMG04.jpg")

cell.imageView?.image = image3

}

return cell
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}


4000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: