当前位置:网站首页>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 :
Set project name testTableView, Language choice Swift, Device selection Universal:
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 .
边栏推荐
- 卷起来,突破35岁焦虑,动画演示CPU记录函数调用过程
- Jupiter notebook shortcut key
- Evolution of Baidu intelligent applet patrol scheduling scheme
- Mysql80 service does not start
- Wechat applet - simple diet recommendation (3)
- 90%的人都不懂的泛型,泛型的缺陷和应用场景
- Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 - 上
- 天龙八部TLBB系列 - 单体技能群伤
- oracle和mysql批量Merge对比
- How do enterprises choose the appropriate three-level distribution system?
猜你喜欢
一种用于干式脑电图的高密度256通道电极帽
硬核,你见过机器人玩“密室逃脱”吗?(附代码)
7 月 2 日邀你来TD Hero 线上发布会
View Slide
高级 OpenCV:BGR 像素强度图
Meitu lost 300 million yuan in currency speculation for half a year. Huawei was exposed to expand its enrollment in Russia. Alphago's peers have made another breakthrough in chess. Today, more big new
苹果 5G 芯片研发失败?想要摆脱高通为时过早
How to use sqlcipher tool to decrypt encrypted database under Windows system
Data visualization platform based on template configuration
如何正确的评测视频画质
随机推荐
宝塔面板MySQL无法启动
Design and exploration of Baidu comment Center
Kotlin Compose 多个条目滚动
[system design] index monitoring and alarm system
TDengine × Intel edge insight software package accelerates the digital transformation of traditional industries
ThreadLocal source code learning
TDengine 连接器上线 Google Data Studio 应用商店
ArcGIS Pro 创建要素
Coordinate system of view
Observation cloud and tdengine have reached in-depth cooperation to optimize the cloud experience of enterprises
Tdengine connector goes online Google Data Studio app store
From "chemist" to developer, from Oracle to tdengine, two important choices in my life
Baidu app's continuous integration practice based on pipeline as code
Cent7 Oracle database installation error
What should we pay attention to when developing B2C websites?
一种用于干式脑电图的高密度256通道电极帽
oracle 多行数据合并成一行数据
天龙八部TLBB系列 - 关于包裹掉落的物品
Matrix processing practice
写入速度提升数十倍,TDengine 在拓斯达智能工厂解决方案上的应用