swift 创建tableView并实现协议



//
//  ViewController2.swift
//  swift_helloword
//
//  Created by Charlie on 15/7/13.
//  Copyright (c) 2015年 Json. All rights reserved.
//

import Foundation
import UIKit

class RootViewController: UIViewController,UITableViewDataSource,UITableViewDelegate { //协议

    var tableView :UITableView? = UITableView( frame: CGRectZero, style: UITableViewStyle.Plain);

    override func viewDidLoad() {
        configUI()
    }
        func configUI(){
            tableView!.frame = CGRect (x: 0, y: 0, width: 320, height: 568)
            self.view.addSubview(tableView!)
            tableView!.delegate = self
            tableView!.dataSource = self //设置代理

        }
     func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 10
    }
     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        var view:UIView = UIView (frame: CGRect (x: 0, y: 0, width: 300, height: 30))
        var la = UILabel (frame: view.frame);
        la.backgroundColor = UIColor.greenColor()
        la.text = "第\(section)+分区"  //分区的名字
        view.addSubview(la)
        return view;
    }
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40 ; //每一个分组的高度
    }
     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cellId:String = "id"
        var cell = tableView.dequeueReusableCellWithIdentifier(cellId) as? UITableViewCell; //?是可选的 就是可能是nil
        if cell == nil {
            cell = UITableViewCell (style: UITableViewCellStyle.Default, reuseIdentifier: cellId)
        }
        cell?.textLabel?.text = String (indexPath.row )
        return cell!// 返回cell 或者 nil
    }
}

 
时间: 2024-07-30 08:09:13

swift 创建tableView并实现协议的相关文章

swift 创建tableView 并实现协议

import UIKit class ViewController2: UIViewController,UITableViewDelegate,UITableViewDataSource{ override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor=UIColor.orangeColor() var myTableView = UITableView(frame: CGRectMake(0, 0, UI

swift:创建表格UITableView

用swift创建单元格和用iOS创建单元格形式基本相同,就是语法上有些异样.swift中调用成员方法不再使用[ ]来发送消息,而是使用.成员方法的形式调用成员函数.这种格式非常类似于java中的点成员运算符.swift中对其他类的引用不用导入头文件.这里就不废话了,现在纯代码创建UITableview实例如下: 具体实例如下: 1.首先用swift创建一个工程Project 2.再用swift创建一个Person类,生成Person.swift文件 3.在Perosn.swift文件中将设置的属

Swift中TableView的基本使用

Xcode6新建一个项目,采用swift创建代码 创建一个ViewController继承UITableViewController 涉及了模型,控制器 模型:ZLPlace.swift class ZLPlace: NSObject { var place = "" var visited = false } tableViewController 控制器 import UIKit class ViewController: UITableViewController { // 静态

使用OC和swift创建系统自带的刷新界面

使用OC和swift创建系统自带的刷新界面 一:swift刷新界面代码: import UIKit class ViewController: UITableViewController { // 用于显示的数据源    var _dataSource:[String] = []        // 加载更多 状态 风火轮    var _aiv:UIActivityIndicatorView!        override func viewDidLoad() {        super.

iOS开发——OC和swift创建系统自带的刷新界面

使用OC和swift创建系统自带的刷新界面 一:swift刷新界面代码: import UIKit class ViewController: UITableViewController { // 用于显示的数据源    var _dataSource:[String] = []        // 加载更多 状态 风火轮    var _aiv:UIActivityIndicatorView!        override func viewDidLoad() {        super.

swift中tableview的使用和注意事项

今天使用swift写了个简单的tableView,语法和用法上跟oc没多大的区别.但是还是有一些细节的地方需要注意一下的. 先上代码 import UIKit class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { var _tableView:UITableView? override func viewDidLoad() { super.viewDidLoad() _tableVie

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 ta

OC与Swift创建pod

Cocoa pods 是iOS最常用的类库管理工具 OC的使用 删除源 sudo gem sources -r https://rubygems.org/ 添加源(使用淘宝的镜像,记住要用https) sudo gem sources -a https://ruby.taobao.org/ 查看是否使用的是淘宝镜像$ gem sources -l # 安装$ sudo gem install cocoapods # 安装成功之后,查看是否是最后版本(目前最新版本是1.0.1)$ pod --ve

Swift标准库中的协议_012_swift协议

//: Playground - noun: a place where people can play import UIKit //--Swift标准库中的协议---// //1.实例的比较:判断两个实例值是否相同 let a = 4, b = 4 a == b //(Int类型的比较) //自定义结构体类型,进行是否相等的比较 struct Games { var winCount : Int var loseCount : Int } let g1 = Games(winCount: 2