Swift创建UITableview

17 篇文章 0 订阅
//
//  ShopViewController.swift
//  ShopCart
//
//  Created by BO on 17/2/16.
//  Copyright © 2017年 xsqBo. All rights reserved.
//

import UIKit

class ShopViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{
    var tableview:UITableView? = nil
    var dataArr = [String]()//创建全局数组
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        //构造数据
        self .SetData()
        //创建表格
        self.setupTableview()
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func SetData()  {
        for i in 0...10 {
           dataArr .append("\(i)")//向数组中添加字符串 数组只能存相同类型的数据
        }
    }
    func setupTableview()  {

        tableview = UITableView(frame:CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height),style:UITableViewStyle.plain)
        tableview?.delegate = self;//?的作用是 当tableview 不为空时 才走后面的delegate
        tableview?.dataSource = self;
        tableview?.backgroundColor = UIColor.brown
        tableview?.separatorStyle = .none
        self.view.addSubview(tableview!);
    }
    /*tableView 代理方法 */
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return self.dataArr.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identify = "cell"

        var cell = tableview?.dequeueReusableCell(withIdentifier: identify)
        if (cell == nil) {
            cell = UITableViewCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: identify)

        }
        cell?.textLabel?.text = "\(self.dataArr[indexPath.row])"
        cell?.textLabel?.textColor = UIColor.red
        cell?.detailTextLabel?.text = "再测试一下"
        return cell!

    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("单元格被点击了")
    }
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

        let alert = UIAlertController(title: "提示",message: "删除后无法恢复,是否继续删除?",preferredStyle: UIAlertControllerStyle.alert)

        let ok = UIAlertAction(title: "确定",style: UIAlertActionStyle.default,handler:{okAction in
            //确定按钮被点击
            self.dataArr.remove(at: (indexPath as NSIndexPath).row)

            tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)



        })

        let cancel = UIAlertAction(title: "取消",style: UIAlertActionStyle.cancel,handler: {cancelAction in


        })

        alert.addAction(ok)
        alert.addAction(cancel)

        self.present(alert, animated: true, completion: nil)


    }

    func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
        //左滑弹出按钮...
        return "删除"
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值