Swift创建UITableview

葛阳华
2023-12-01
//
//  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.
    }
    */

}
 类似资料: