SWIFT UITableView的基本用法

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        // Override point for customization after application launch.
        self.window!.backgroundColor = UIColor.whiteColor()

        let navigation = UINavigationController(rootViewController: RootViewController())
        self.window?.rootViewController = navigation

        self.window!.makeKeyAndVisible()
        return true
    }

}
import UIKit

class RootViewController: UIViewController {

    override func loadView() {
        super.loadView()
        //初始化UITableView
        let tableView = UITableView()
        tableView.frame = UIScreen.mainScreen().bounds
        tableView.dataSource = self
        tableView.delegate = self
        self.view.addSubview(tableView)
    }
    //懒加载数据
    lazy var datas:[String] = {
        return ["是雨是泪","分分钟需要你","伤心太平洋","曾经最痛","飘雪"]
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "UITableView的基本用法"
    }
}

extension RootViewController:UITableViewDelegate,UITableViewDataSource
{
    //区的个数
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    //在相应区中cell的个数
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    // cell的高度
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 60
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //在重用机制里取出cell
        var cell = tableView.dequeueReusableCellWithIdentifier("cell")
        //如果cell为空则创建
        if cell == nil {
            cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
        }
        //设置数据
        cell?.textLabel?.text = datas[indexPath.row]
        return cell!
    }

}
时间: 2024-08-26 14:11:20

SWIFT UITableView的基本用法的相关文章

Swift - UITableView展开缩放动画

效果 源码 https://github.com/YouXianMing/Swift-Animations // // HeaderViewTapAnimationController.swift // Swift-Animations // // Created by YouXianMing on 16/8/9. // Copyright © 2016年 YouXianMing. All rights reserved. // import UIKit class HeaderViewTapA

swift UITableView使用

1.新建RootViewController类 // // RootViewController.swift // UITableViewDemo // // Created by 赵超 on 14-6-21. // Copyright (c) 2014年 赵超. All rights reserved. // import UIKit class RootViewController: UIViewController,UITableViewDelegate, UITableViewDataS

Swift - UITableView状态切换效果

效果 源码 https://github.com/YouXianMing/Swift-Animations // // TableViewTapAnimationController.swift // Swift-Animations // // Created by YouXianMing on 16/8/7. // Copyright © 2016年 YouXianMing. All rights reserved. // import UIKit class TableViewTapAni

IOS SWIFT UITableView 实现简单微博列表

// // Weibo.swift // UITableViewCellExample // // Created by XUYAN on 15/8/15. // Copyright (c) 2015年 com.world. All rights reserved. // import Foundation class Weibo { //属性 var id : UInt32 var img : String! var username : String! var mbtype : String

UITableView的简单用法

#import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //初始化一个ViewController的一个对象 ViewController *view = [

黑马程序员——UITableView的详细用法

------<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培训..Net培训</a>.期待与您交流! ------- UITableView内置了两种样式:UITableViewStylePlain,UITableViewStyleGrouped <UITableViewDataSource,UITableViewDelegate>里的方法:

ios初识UITableView及简单用法二(模型数据)

// // ViewController.m // ZQRTableViewTest // // Created by zzqqrr on 17/8/24. // Copyright (c) 2017年 zzqqrr. All rights reserved. // #import "ViewController.h" #import "ZQRCarGroup.h" @interface ViewController () <UITableViewDataSo

Swift反射API及其用法

猛戳查看最终版@SwiftGG 尽管 Swift 一直在强调强类型.编译时安全和静态调度,但它的标准库仍然提供了反射机制.可能你已经在很多博客文章或者类似Tuples.Midi Packets 和 Core Data 的项目中见过它.也许你刚好对在项目中使用反射机制感兴趣,或者你想更好滴了解反射可以应用的领域,那这篇文章就正是你需要的.文章的内容是基于我在德国法兰克福 Macoun会议上的一次演讲,它对 Swift 的反射 API 做了一个概述. API 概述 理解这个主题最好的方式就是看API

Swift 另类判断语句用法 button.selected = index &lt; rating

很简洁的用法: index < rating return true or false for (index, button) in ratingButtons.enumerate() { // If the index of a button is less than the rating, that button should be selected. button.selected = index < rating } 大大简化了语句,可读性很强.对比老写法: button.select