swift - UITableView 使用


swift大体看了一遍,写个最常用的UItableview demo,希望对大家有所帮助,不对的地方也请大家批评指正。

1.用xcode建立一个基于swift语言的应用程序(xcode6.0可以进行oc和swift语言选择的)。

2.添加两个UIVIewcontroller、添加一个继承NSObject的自定义类,这样整个工程建立起来了,如下图:

  


3.先来看看StudentObject.swift代码:

import UIKit

class StudentObject: NSObject
{
    var sName:String?
    var sAge:Int?
    var sAddress:String?

    init(Name : String, Age : Int, Address : String)
    {
        sName = Name;
        sAge = Age;
        sAddress = Address;
    }

}


4.再来看看TestTableViewController.swift代码:  

import UIKit
import Foundation

class TestTableViewController: UIViewController,UITableViewDataSource,UITableViewDelegate
{
    
    
    @IBOutlet var ui_myTable : UITableView = nil
    var tableData = StudentObject[]();

    init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?)
    {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        // Custom initialization
    }

    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        self.title = "第一个页面";
        
        for index in 1...10
        {
            var tmpObj = StudentObject(Name: "lcc _ " + String(index) ,Age: index,Address: "address _ " + String(index));
            tableData += tmpObj;
        }
        

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
    {
        return tableData.count;
    }
    
    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
    {
        let identifierString = "identifierString"
        
        var cell:UITableViewCell! = tableView.dequeueReusableCellWithIdentifier(identifierString) as? UITableViewCell;
        
        if cell == nil
        {
            cell = UITableViewCell(style: UITableViewCellStyle.Default,reuseIdentifier: identifierString);
            
            println("%d",indexPath.row);
        }
        
        var tmpObj:StudentObject = tableData[indexPath.row];
        cell.textLabel.text = tmpObj.sName;
        
        return cell;
    }
    
    func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat
    {
        return 100;
    }
    
    //这里绑定单击事件和oc中一样
    @IBAction func pushTapped(sender : AnyObject)
    {
        var secVc : SecondViewController = SecondViewController(nibName:nil,bundle: nil);
        self.navigationController.pushViewController(secVc,animated: true);
    }
}


说明一下:

var cell:UITableViewCell! = tableView.dequeueReusableCellWithIdentifier(identifierString) as? UITableViewCell;

这句话有太多知识点:


a.选择类型和隐形选择类型。

b.?和!的区别。

c.as?和  as  是什么意思?

d.类型转换(大家可以自己把as后面的问好去掉,在运行试试)。


大家可以带着这些疑问可以区看看swift中文教程。不懂的,可以给我留言。


5.最后看看AppDelegate.swift

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate
{
                            
    var window: UIWindow  = UIWindow(frame: UIScreen.mainScreen().bounds)
    var testTable:TestTableViewController?

    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
        
        self.testTable = TestTableViewController(nibName:nil, bundle:nil);
        
        var nav:UINavigationController = UINavigationController(rootViewController: self.testTable);
        
        self.window.rootViewController = nav;
        
        self.window.backgroundColor = UIColor.whiteColor()
        self.window.makeKeyAndVisible()
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
    }

    func applicationDidEnterBackground(application: UIApplication) {
    }

    func applicationWillEnterForeground(application: UIApplication) {
    }

    func applicationDidBecomeActive(application: UIApplication) {
    }

    func applicationWillTerminate(application: UIApplication) {
    }


}

源文件路径:http://download.youkuaiyun.com/detail/folish_audi/7516397



       

期末大作业基于python的足球运动员数据分析源码+数据集(高分项目),个人经导师指导并认可通过的高分设计项目,评审分98分,项目中的源码都是经过本地编译过可运行的,都经过严格调试,确保可以运行!主要针对计算机相关专业的正在做大作业、毕业设计的学生和需要项目实战练习的学习者,资源项目的难度比较适中,内容都是经过助教老师审定过的能够满足学习、使用需求,如果有需要的话可以放心下载使用。 期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于pyth
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值