• Swift基础--tableview练习


    新手练习Swift版tableview

    //  ViewController.swift

    //  Swift_lianxi

    //  Created by dllo on 16/1/7.

    //  Copyright © 2016年 z_han. All rights reserved.

    //

    import UIKit

    class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

        var tableView :UITableView?

        var dataArray :NSMutableArray?

        

        override func viewDidLoad() {

            super.viewDidLoad()

            // Do any additional setup after loading the view, typically from a nib.

            self.dataArray = NSMutableArray()

            

            tableView = UITableView.init(frame: self.view.frame, style: UITableViewStyle.Plain)

            tableView?.delegate = self

            tableView?.dataSource = self

            self.view.addSubview(tableView!)

            tableView?.registerClass(TableViewCell.self, forCellReuseIdentifier: "TableViewCell")

            tableView?.rowHeight = 110

            

            self.getData()

            

        }

        func getData(){

            let manager :AFHTTPSessionManager = AFHTTPSessionManager()

            // 支持的接口类型

            manager.responseSerializer.acceptableContentTypes = NSSet(objects:"text/plain","text/json","application/json","text/javascript","text/html") as? Set<String>

            manager.GET("http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php", parameters: nil, progress: nil, success: { (tast: NSURLSessionDataTask, responseObject:AnyObject?) -> Void in

    //                print(responseObject)

                let responseObject :NSDictionary = responseObject as! NSDictionary

                let resultArray :NSArray = responseObject.objectForKey("result") as!NSArray

    //            print(resultArray)

                for dic in resultArray{

                    let resultDic :NSDictionary = dic as! NSDictionary

                    let model :Model = Model()

                    // 赋值

                    model.setValuesForKeysWithDictionary(resultDic as![String : AnyObject])

                    self.dataArray?.addObject(model)

                }

                    self.tableView?.reloadData()

                }) { (task:NSURLSessionDataTask?, error :NSError) -> Void in

                    print("失败 (error)")

            }

        }

        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

            

            return (self.dataArray?.count)!;

        }

        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

            let cell :TableViewCell? = tableView.dequeueReusableCellWithIdentifier("TableViewCell") as? TableViewCell

            let model = self.dataArray![indexPath.row]

            cell?.model = model as? Model

            cell?.selectionStyle = UITableViewCellSelectionStyle.None

            return cell!

        }

        func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {      

            let detailVC :DetailViewController? = DetailViewController()

            

            self.presentViewController(detailVC!, animated: true, completion: nil)

            }

    //  TableViewCell.swift

    //  Swift_lianx

    //

    //  Created by dllo on 16/1/7.

    //  Copyright © 2016年 z_han. All rights reserved.

    //

    import UIKit

    class TableViewCell: UITableViewCell {

        var imageVieg :UIImageView?

        var titleLabel :UILabel?

        var bgImage :UIImageView?

        

        var model :Model?{

            set(newModel){

                let url :NSURL = NSURL.init(string: (newModel?.pic_url)!)!

                self.imageVieg?.sd_setImageWithURL( url, placeholderImage: UIImage(named: "picholder")!)

                self.titleLabel?.text = newModel?.movieName

            }

            get{

                return self.model

            }

        }

        

        override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

            super.init(style: style, reuseIdentifier: reuseIdentifier)

            // 背景

            bgImage = UIImageView.init(image: UIImage(named: "bg_eventlistcell"))

            bgImage?.frame = CGRectMake(10, 10, 375 - 20, 100)

            self.contentView.addSubview(bgImage!)

            

            // 图片

           imageVieg = UIImageView.init(image: UIImage(named: "picholder"))

            imageVieg?.frame = CGRectMake(10, 10, 60, 80)

            bgImage?.addSubview(imageVieg!)

            // 标题

            titleLabel = UILabel.init(frame: CGRectMake(90, 20, 375 - 10-10-60-20, 60))

            bgImage?.addSubview(titleLabel!)

            

        }

        required init?(coder aDecoder: NSCoder) {

            fatalError("init(coder:) has not been implemented")

        }

        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

        }

  • 相关阅读:
    RabbitMQ + PHP (二)AMQP拓展安装
    RabbitMQ + PHP (一)入门与安装
    使用 Selenium 实现基于 Web 的自动化测试
    Selenium私房菜系列4 -- Selenium IDE的使用
    解决火狐浏览器安装不上Selenium IDE插件“此附加组件无法安装”
    (技术分享) 解决 Firefox 显示“已阻止载入混合活动内容”的问题
    MyEclipse打开 HTML 报错Failed to create the part's controls
    python2x与python3x的区别
    Python基础总结
    Mycat 读写分离+分库分表
  • 原文地址:https://www.cnblogs.com/z-han49888/p/5114331.html
Copyright © 2020-2023  润新知