[Swift-Learning]之UITableView的简单使用

简介

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)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,200评论 4 61
  • 我想忘了 又怕忘了
    愚初二阅读 342评论 0 0
  • 作者:浅戈易 我想谈谈我的感受了。 简书,是我在一年多前通过网络获知的,一年多后,也就这几天的时间,我把原先用过的...
    浅戈易阅读 250评论 1 9
  • 昨天已经十一点多了,我正准备睡觉,一个朋友微信头像开始闪了起来,于是,我揉了揉已经有一半睡意的双眼,开始聊了起来。...
    简鸽阅读 581评论 9 6
  • 二零一七年七月十七号晚 梦到你对我超级超级好 还带我去玩 在梦里 我们只差一句话 二零一七年七月二十日 在死党家删...
    降降阅读 193评论 0 0