Swift UITableView

  UITableView,除了语法不一样,其他地方和OC都没有太大区别。

  在处理TableView之前呢,数据是万万不能少的。然后我就写了一个数据请求的方法。因为还没有学会使用Model类,所以,用的是最原始的赋值方法。

    //网络请求
    func requestURL(urlString:String){
        let url:NSURL = NSURL(string: urlString)!
        let request:NSURLRequest = NSURLRequest(URL: url)
        
        NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response, data, error) -> Void in
            if ((error) != nil) {
                print(error)
            }else{
                print(response)
                let jsonDic = try? NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments)
                let dataDic:NSDictionary = jsonDic?.objectForKey("data") as! NSDictionary
                self.dataDic = dataDic
                self.tableView?.reloadData()
            }
        }
    }

  然后,在ViewDidLoad里面调用就可以了。

    override func viewDidLoad() {
        self.view.backgroundColor = UIColor.yellowColor()
        
        requestURL("http://118.192.66.48/jndemo/api/v1.0/cityList.do")//http://118.192.66.48/jndemo/api/v1.0/cityList.do
        
        tableView = UITableView.init(frame: self.view.bounds, style: UITableViewStyle.Grouped)
        tableView!.delegate = self
        tableView!.dataSource = self
        self.view.addSubview(tableView!)
        tableView!.backgroundColor = UIColor.yellowColor()
        tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }
  • 首先,我们可以将tableView定义为公有属性:
class SecondViewController: UIViewController ,UITableViewDataSource,UITableViewDelegate {
    var tableView:UITableView?
}

  这样,在下面的时候,我们就可以用self来调用这个tableView了。

  • 初始化
        tableView = UITableView.init(frame: self.view.bounds, style: UITableViewStyle.Grouped)
        tableView!.delegate = self
        tableView!.dataSource = self
        self.view.addSubview(tableView!)
        tableView!.backgroundColor = UIColor.yellowColor()
        tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
  • 实现TableViewDelegate和TableViewDataSource
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return self.dataDic.allKeys.count;
    }
    
    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        let sectionArr:NSArray = self.dataDic.allKeys
        let sectionText = sectionArr.objectAtIndex(section)
        return sectionText as? String
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = self.tableView!.dequeueReusableCellWithIdentifier("cell")
        let sectionArr:NSArray = self.dataDic.allKeys
        let sectionText:String = sectionArr.objectAtIndex(indexPath.section) as! String
        let rowArr:NSMutableArray = self.dataDic.objectForKey(sectionText) as! NSMutableArray
        let rowDic:NSDictionary = rowArr.objectAtIndex(indexPath.row) as! NSDictionary
        let cityName:String  = rowDic.objectForKey("city") as! String
        cell?.textLabel?.text = cityName
        return cell!
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let sectionArr:NSArray = self.dataDic.allKeys
        let sectionText:String = sectionArr.objectAtIndex(section) as! String
        let rowArr:NSMutableArray = self.dataDic.objectForKey(sectionText) as! NSMutableArray
        return rowArr.count
    }

  这样,这个tableView就出来了。

  写到这里,我发现,看之前的文档还是挺有用的,我要不还是滚回去看原来的那个文档吧。

原文地址:https://www.cnblogs.com/tanglimei/p/5130159.html