当前位置:网站首页>Swift tableview style (I) system basic

Swift tableview style (I) system basic

2022-07-05 10:09:00 cppphp

open Xcode, Create a new project :

choice iOS\Application\Single View Application Create a single view application :

swift create tableview project4

swift create tableview project3

Set project name testTableView, Language choice Swift, Device selection Universal:

swift create tableview project2

After the project is created , We can see in the project directory structure that there is only one storyboard file :

Main.storyboard File is a general storyboard file , It can adapt to all current screen sizes Apple Mobile devices . Open the file , You'll see a View Controller, And an interface dimension that we are not familiar with :

First , We turn on Main.storyboard file , From component library (Object Library) Choose from Table View Drag and drop to View Controller in .

Select the just dragged Table View, add to 4 A constraint , Make it fill the screen . 

then , Let's start from the component library (Object Library) Choose from  Table View Cell And drag to Table View On .

Then select Basic, Enter basicCell. 

  Select Table View entry , Right click the mouse to pop up the black window below , Click the respectively dataSource and delegate The circle on the right , And drag to the position of the red arrow, and then release the mouse .

stay ViewContoler.swift Enter the following code  


import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    var teststring: Array<String> = ["This is test1 of TabllView"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    //  Data source method ,  How many groups are returned 
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1;
    }
    
    //  How many rows are there in each group 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return teststring.count
    }
    
    //  What does each line show 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "basicCell", for: indexPath)
        
        cell.textLabel?.text = teststring[indexPath.row]
        return cell
    }
}

Click on xcode The operation button in the upper left corner , The following screen will be displayed , A simple Table View It's done. .

Now let's add cell Dynamically add and delete   function .

Let's add one at the bottom of the interface mentioned above View And put two buttons on it , Here's the picture

choice + Button and right-click to pop up the following black window , And then Touch Up Inside Press the left mouse button in the corresponding circle on the right and drag it to the red arrow , Releasing the right button will pop up the window at the bottom of the figure below Name Enter... In the input box addTest, In its Type Select... From the combo box UIButton, And then click Connect Button .

- The operation steps of the button are the same as + Button is the same. , The difference is just Name Enter... In the input box subTest, The others are the same .

And then again ..., It's the same old story , Let's see the following figure and operate by ourselves  

The three red boxes in the figure below are dragged from above , Two of the drag buttons have function codes entered . 

The completed code is as follows : 

//
//  ViewController.swift
//  testTableView
//
//  Created by xxxx on 2019/8/31.
//  Copyright  2019 xxxxx. All rights reserved.
//

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    var teststring: Array<String> = ["This is test1 of TabllView"]

    @IBOutlet weak var vwTable: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    //  Data source method ,  How many groups are returned 
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1;
    }
    
    //  How many rows are there in each group 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return teststring.count
    }
    
    //  What does each line show 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath)
        
        cell.textLabel?.text = teststring[indexPath.row]
        return cell
    }

    @IBAction func addTest(_ sender: UIButton) {
        let count = teststring.count + 1
        teststring.append("This is test" + String(count) + " of TabllView")
        vwTable.reloadData()
    }
    
    @IBAction func subTest(_ sender: UIButton) {
        teststring.removeLast()
        vwTable.reloadData()
    }
}

Let's run the program again , adopt + and - Buttons can be added or deleted dynamically cell 了 , You can test . 

creat swift tableview project

原网站

版权声明
本文为[cppphp]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140534598032.html