先简单的写个json格式的问价
[
{"head":"1","title":"寻物启事","detailTitle":"啊飒飒发你撒开了放哪","time":"1小时前","img":"1","num":"45","type":"0"},
{"head":"1","title":"潮女搭配师","detailTitle":"啊飒飒发你撒开了放哪啊飒飒发你撒开了放哪啊飒飒发你撒开了放哪啊飒飒发你撒开了放哪啊飒飒发你撒开了放哪啊飒飒发你撒开了放哪啊飒飒发你撒开了放哪啊飒飒发你撒开了放哪啊飒飒发你撒开了放哪啊飒飒发你撒开了放哪啊飒飒发你撒开了放哪","time":"1小时前","img":["1","1","1","1","1","1","1","1"],"num":"34","type":"1"},
{"head":"1","title":"星妈萌娃","detailTitle":"啊飒飒发你撒开了放哪","time":"刚刚","img":"1","num":"56","type":"2"}
]
接下来,我们需要创建三个继承自NSObject的model
然后根据json文件在里面需要定义一些与json文件匹配的变量
这里我就写一个里面的
var headImg:String
var title:String
var detailTitle:String
var img:String
var time:String
var num:String
var type:String
init(headImg:String,title:String,detailTitle:String,img:String,time:String,num:String,type:String) {
self.headImg = headImg
self.title = title
self.time = time
self.detailTitle = detailTitle
self.img = img
self.num = num
self.type = type
不一样的地方就是json文件里面的格式不太一样,大家改一下就好了
然后我们继续创建三个cell类,继承自UITableviewCell
里面只有一个方法
func setCellWithData(model:OneMD) {
self.headImgView.image = UIImage(named: model.headImg)
self.titleLabel.text = model.title
self.timeLabel.text = model.time
self.detailLabel.text = model.detailTitle
self.imgView.image = UIImage(named: model.img)
self.numLabel.text = model.num
}
这个里面写的是我们自己拖的控件,名字根据我们自己的喜欢来定义就好
然后我们在Viewcontroller里面请求一下解析
let path = Bundle.main.path(forResource: "daren", ofType: "json")
let url = URL(fileURLWithPath: path!)
do {
let data = try Data(contentsOf: url)
let jsonData:Any = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers)
// print(jsonData)
let jsonDic:NSArray = jsonData as! NSArray
print(jsonDic)
} catch let error as Error? {
print(error as Any)
}
然后就是我们指定位置的分段里面的代码了
let mArr:NSMutableArray = []
var cellHight:CGFloat = 0
var tv:UITableView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
NotificationCenter.default.addObserver(self, selector: #selector(test), name: NSNotification.Name(rawValue:"cellHight"), object: nil)
view.backgroundColor = UIColor.white
let path = Bundle.main.path(forResource: "daren", ofType: "json")
let url = URL(fileURLWithPath: path!)
do {
let data = try Data(contentsOf: url)
let jsonData:Any = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers)
// print(jsonData)
let jsonArr:NSArray = jsonData as! NSArray
print(jsonArr)
for item in jsonArr {
let k:NSDictionary = item as! NSDictionary
if k.object(forKey: "type") as! String == "0"{
let md:OneMD = OneMD(headImg: k.object(forKey: "head") as! String, title: k.object(forKey: "title") as! String, detailTitle: k.object(forKey: "detailTitle") as! String, img: k.object(forKey: "img") as! String, time: k.object(forKey: "time") as! String, num: k.object(forKey: "num") as! String, type: k.object(forKey: "type") as! String)
mArr.add(md)
}else if k.object(forKey: "type") as! String == "1" {
let md:TwoMD = TwoMD(headImg: k.object(forKey: "head") as! String, title: k.object(forKey: "title") as! String, detailTitle: k.object(forKey: "detailTitle") as! String, img: k.object(forKey: "img") as! NSMutableArray, time: k.object(forKey: "time") as! String, num: k.object(forKey: "num") as! String, type: k.object(forKey: "type") as! String)
mArr.add(md)
}else{
let md:ThreeMD = ThreeMD(headImg: k.object(forKey: "head") as! String, title: k.object(forKey: "title") as! String, detailTitle: k.object(forKey: "detailTitle") as! String, img: k.object(forKey: "img") as! String, time: k.object(forKey: "time") as! String, num: k.object(forKey: "num") as! String, type: k.object(forKey: "type") as! String)
mArr.add(md)
}
}
} catch let error as Error? {
print(error as Any)
}
tv = UITableView(frame:CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height-120), style: .plain)
view.addSubview(tv!)
tv!.delegate = self
tv!.dataSource = self
tv!.register(UINib(nibName: "OneCell", bundle: Bundle.main), forCellReuseIdentifier: "cell1")
tv?.register(UINib(nibName: "TwoCell", bundle: Bundle.main), forCellReuseIdentifier: "cell2")
tv!.register(UINib(nibName: "ThreeCell", bundle: Bundle.main), forCellReuseIdentifier: "cell3")
tv!.tableFooterView = UIView()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return mArr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = mArr[indexPath.row]
if item is OneMD{
let md:OneMD = mArr[indexPath.row] as! OneMD
let cell:OneCell = tableView.dequeueReusableCell(withIdentifier: "cell1") as! OneCell
cell.setCellWithData(model: md)
return cell
} else if item is TwoMD{
let md:TwoMD = mArr[indexPath.row] as! TwoMD
let cell:TwoCell = tableView.dequeueReusableCell(withIdentifier: "cell2") as! TwoCell
cell.setCellWithData(model: md)
print(cellHight)
return cell
}else{
let md:ThreeMD = mArr[indexPath.row] as! ThreeMD
let cell:ThreeCell = tableView.dequeueReusableCell(withIdentifier: "cell3") as! ThreeCell
cell.setCellWithData(model: md)
return cell
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let item = mArr[indexPath.row]
if item is TwoMD{
return cellHight+10
} else{
return 200
}
}
@objc func test(nofi : Notification){
cellHight = nofi.userInfo!["post"] as! CGFloat
tv?.reloadData()
/// 移除通知
NotificationCenter.default.removeObserver(self)
}
大家根据json文件的不同,改变相应的地方就好了