• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

swift表格真假数据

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1.导入第三方PageTitleView
2.daren.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”}
]
3.在AppDelegate.swift中添加:

let oneNav = UINavigationController(rootViewController: WeiTaoVC())
    oneNav.tabBarItem = UITabBarItem(title: "首页", image: UIImage(named: ""), selectedImage: UIImage(named: ""))
    
    let TwoNav = UINavigationController(rootViewController: oneViewController())
    TwoNav.tabBarItem = UITabBarItem(title: "我听", image: UIImage(named: ""), selectedImage: UIImage(named: ""))
    
    let ThreeNav = UINavigationController(rootViewController: twoViewController())
    ThreeNav.tabBarItem = UITabBarItem(title: "播放", image: UIImage(named: ""), selectedImage: UIImage(named: ""))
    
    let tav = UITabBarController()
    tav.viewControllers = [oneNav,TwoNav,ThreeNav]
    tav.selectedViewController = TwoNav
    self.window?.rootViewController = tav

4.oneViewController、twoViewController继承于UIViewController
加入代码:super.viewDidLoad()

5.创建VCTwo继承于UIViewController
代码:
import UIKit

class VCTwo: UIViewController {
private lazy var pageTitleView: MFPageTitleView = {

    let config = MFPageTitleViewConfig()
    config.titleColor = colorWithRGB(r: 43, g: 43, b: 43)
    config.titleSelectedColor = colorWithRGB(r: 211, g: 0, b: 0)
    config.titleFont = UIFont.systemFont(ofSize: 14, weight: .regular)
    config.indicatorColor = colorWithRGB(r: 211, g: 0, b: 0)
    let pageTitleView = MFPageTitleView(frame: CGRect(x: 0, y: navHeight, width: SCREEN_WIDTH, height: 41), titles: ["全部", "上新", "视频直播", "特别关注", "达人"], config: config)
    pageTitleView.pageTitleViewDelegate = self
    return pageTitleView
}()

private lazy var pageContentView: MFPageContentView = {
    var childControllers = [UIViewController]()
    for k in 0..<5 {
        var vc = UIViewController()
        let red: CGFloat = CGFloat(arc4random() % 256)
        let green: CGFloat = CGFloat(arc4random() % 256)
        let blue: CGFloat = CGFloat(arc4random() % 256)
        vc.view.backgroundColor = UIColor(red: red / 255.0, green: green / 255.0, blue: blue / 255.0, alpha: 1.0)
        if k==4{
            vc = WeiTaoVC()
        }
        childControllers.append(vc)
    }
    
    let pageContentViewY = pageTitleView.frame.maxY
    let pageContentView = MFPageContentView(frame: CGRect(x: 0, y: pageContentViewY, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-pageContentViewY), parentVC: self, childVCs: childControllers)
    pageContentView.pageContentViewDelegate = self
    return pageContentView
}()

override func viewDidLoad() {
    super.viewDidLoad()
    self.title="111"
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: nil)
    self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: nil)
    view.backgroundColor = UIColor.white
    view.addSubview(pageTitleView)
    view.addSubview(pageContentView)
    
}

}

extension VCTwo: MFPageTitleViewDelegate, MFPageContentViewDelegate {

func selectedIndexInPageTitleView(pageTitleView: MFPageTitleView, selectedIndex: Int) {
    self.pageContentView.setPageContentViewCurrentIndex(currentIndex: selectedIndex)
}
func pageContentViewScroll(progress: CGFloat, originalIndex: Int, targetIndex: Int) {
    self.pageTitleView.setPageTitleView(progress: progress, originalIndex: originalIndex, targetIndex: targetIndex)
}

}
6.创建WeiTaoVC继承于UIViewController
代码:
import UIKit

class WeiTaoVC: UIViewController,UITableViewDelegate,UITableViewDataSource {

let mArr:NSMutableArray = []
var cellHight:CGFloat = 0


override func viewDidLoad() {
    super.viewDidLoad()
    
    // Do any additional setup after loading the view.
    
    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)
    }
    
    
    let tv = UITableView(frame: self.view.frame, 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.tableFooterView = UIView()
}

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

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)
        cell.titleLabel?.text="1"
        cell.imgView?.image=UIImage(named: "2")
        return cell
    } else{
        let md:TwoMD = mArr[indexPath.row] as! TwoMD
        
        let cell:TwoCell = tableView.dequeueReusableCell(withIdentifier: "cell2") as! TwoCell
        cellHight = cell.setCellWithData(model: md)
        print(cellHight)
        return cell
    }
    
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    
    let item = mArr[indexPath.row]
    
    if item is OneMD{
        
        return 200
    } else{
        
        return cellHight
    }
    
}

}
7.创建OneMD继承于NSObject
代码:
import UIKit

class OneMD: NSObject {

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
    
}

}
8.创建TwoMD继承于NSObject
代码:
import UIKit

class TwoMD: NSObject {

var headImg:String
var title:String
var detailTitle:String
var img:NSMutableArray
var time:String
var num:String
var type:String
init(headImg:String,title:String,detailTitle:String,img:NSMutableArray,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
    
}

}

9.创建OneCell继承于UITableViewCell、选中Also create file

代码:
import UIKit

class OneCell: UITableViewCell {

@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var imgView: UIImageView!
func setCellWithData(model:OneMD) {
    
    
    self.titleLabel.text = model.title
    
    self.imgView.image = UIImage(named: model.img)
   
}
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
}

}

20.创建TwoCell继承于UITableViewCell、选中Also create file

代码:
import UIKit

class TwoCell: UITableViewCell {

@IBOutlet weak var headImgView: UIImageView!
@IBOutlet weak var detailLabel: UILabel!
func setCellWithData(model:TwoMD)->CGFloat {
    
    self.headImgView.image = UIImage(named: model.headImg)
    self.detailLabel.font=UIFont.systemFont(ofSize: 15)
     let labHight:CGFloat  = autoLabel(laebl: self.detailLabel,lineHeight:6.00)
    print(labHight)
    return labHight+150
}

//加载自适应label
func autoLabel(laebl:UILabel,lineHeight:CGFloat)->CGFloat{
    laebl.numberOfLines=0
    laebl.lineBreakMode = NSLineBreakMode.byWordWrapping
    let text:String = laebl.text!//获取label的text
    laebl.attributedText = self .getAttributeStringWithString(text, lineSpace: lineHeight)
    let fontSize = CGSize(width: laebl.frame.width, height: laebl.font.lineHeight)
    let rect:CGSize = text.boundingRect(with: fontSize, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: laebl.font], context: nil).size;
    laebl.frame = CGRect(x:laebl.frame.origin.x,y:laebl.frame.origin.y+12,width: rect.width, height: rect.height)
    laebl.sizeToFit()
    
    return rect.height
}

//设置行间距
fileprivate func getAttributeStringWithString(_ string: String,lineSpace:CGFloat
    ) -> NSAttributedString{
    let attributedString = NSMutableAttributedString(string: string)
    let paragraphStye = NSMutableParagraphStyle()
    //调整行间距
    paragraphStye.lineSpacing = lineSpace
    let rang = NSMakeRange(0, CFStringGetLength(string as CFString!))
    attributedString .addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStye, range: rang)
    return attributedString
}
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人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift-技巧(二)模糊脸部功能发布时间:2022-07-13
下一篇:
swift1.2语言函数和闭包函数介绍发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap