美文网首页
[Swift-Learning]之UITableView的简单使

[Swift-Learning]之UITableView的简单使

作者: hu9134 | 来源:发表于2016-05-12 15:51 被阅读153次

简介

Swift也出来好长时间了,一直也没抽出时间来学习,最近有时间学习一下Swift,记录学习的点滴,每天进步一点点.

UITableView的使用

UITableViewController应该是使用最多的展示界面了,其使用方法也很简单,注意的地方就是设置数据源和代理,cell的重用(这个应该是最重要的了)



//
//  ViewController.swift
//  UITableView
//
//  Created by Apple on 16/5/12.
//  Copyright © 2016年 hukezhu. All rights reserved.
//

import UIKit

class ViewController: UIViewController{
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.lightGrayColor();
        
    }
    
    override func loadView() {
        
        let tableView = UITableView()
        tableView.frame = UIScreen.mainScreen().bounds
        tableView.dataSource = self
        tableView.delegate = self
        view = tableView
    }
    
    lazy var dataList:[String] = {
        
        return ["张一丰","张二丰","张三丰","张四丰"]
    }()
    
}
//MARK: - UITableViewDelegate AND UITableViewDataSource
extension ViewController: UITableViewDelegate,UITableViewDataSource{
    
    //返回每一个section有多少行
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataList.count
    }
    
    //cell显示的内容
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("UITableViewCell")
        
        if cell == nil {
            cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "UITableViewCell")
        }
        
        cell?.textLabel?.text = dataList[indexPath.row]
        
        return cell!
    }
    
    //选中cell执行的操作
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        //取消选中的效果
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        
        //UIAlertView在iOS9过期了,建议以后使用UIAlertController
        let alertCtrl = UIAlertController(title: "名称", message: dataList[indexPath.row], preferredStyle: UIAlertControllerStyle.Alert)
        
        let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel) { (UIAlertAction) in
            print("点击了取消按钮")
        }
        let okAction = UIAlertAction(title: "确定", style: UIAlertActionStyle.Default) { (UIAlertAction) in
            print("点击了确定按钮")
        }
        
        alertCtrl.addAction(cancelAction)
        alertCtrl.addAction(okAction)
        
        self.presentViewController(alertCtrl, animated: true, completion: nil)
        
    }
    
}

UIAlertController的使用

关于UIAlertController的用法,之前看过一篇文章,介绍的很详细,还有OC版本和Swift版本,有兴趣可以看一下.http://www.cocoachina.com/ios/20141126/10320.html

 //UIAlertView在iOS9过期了,建议以后使用UIAlertController
        let alertCtrl = UIAlertController(title: "名称", message: dataList[indexPath.row], preferredStyle: UIAlertControllerStyle.Alert)
        
        let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel) { (UIAlertAction) in
            print("点击了取消按钮")
        }
        let okAction = UIAlertAction(title: "确定", style: UIAlertActionStyle.Default) { (UIAlertAction) in
            print("点击了确定按钮")
        }
        
        alertCtrl.addAction(cancelAction)
        alertCtrl.addAction(okAction)
        
        self.presentViewController(alertCtrl, animated: true, completion: nil)

相关文章

网友评论

      本文标题:[Swift-Learning]之UITableView的简单使

      本文链接:https://www.haomeiwen.com/subject/oxrprttx.html