当前位置:网站首页>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 .
边栏推荐
- Roll up, break through 35 year old anxiety, and animate the CPU to record the function call process
- Solve liquibase – waiting for changelog lock Cause database deadlock
- How to implement complex SQL such as distributed database sub query and join?
- 【系统设计】指标监控和告警系统
- 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
- Those who are good at using soldiers, hide in the invisible, and explain the best promotional value works in depth in 90 minutes
- To bring Euler's innovation to the world, SUSE should be the guide
- 一种用于干式脑电图的高密度256通道电极帽
- La voie de l'évolution du système intelligent d'inspection et d'ordonnancement des petites procédures de Baidu
- 从“化学家”到开发者,从甲骨文到 TDengine,我人生的两次重要抉择
猜你喜欢
美图炒币半年亏了3个亿,华为被曝在俄罗斯扩招,AlphaGo的同类又刷爆一种棋,今日更多大新闻在此...
To bring Euler's innovation to the world, SUSE should be the guide
How to choose the right chain management software?
苹果 5G 芯片研发失败?想要摆脱高通为时过早
The popularity of B2B2C continues to rise. What are the benefits of enterprises doing multi-user mall system?
百度智能小程序巡檢調度方案演進之路
Idea debugs com intellij. rt.debugger. agent. Captureagent, which makes debugging impossible
Observation cloud and tdengine have reached in-depth cooperation to optimize the cloud experience of enterprises
【小技巧】获取matlab中cdfplot函数的x轴,y轴的数值
H. 265 introduction to coding principles
随机推荐
Pagoda panel MySQL cannot be started
How to choose the right chain management software?
To bring Euler's innovation to the world, SUSE should be the guide
如何獲取GC(垃圾回收器)的STW(暫停)時間?
一文读懂TDengine的窗口查询功能
The essence of persuasion is to remove obstacles
Tdengine already supports the industrial Intel edge insight package
RMS TO EAP通过MQTT简单实现
硬核,你见过机器人玩“密室逃脱”吗?(附代码)
天龙八部TLBB系列 - 关于技能冷却和攻击范围数量的问题
解决idea调试过程中liquibase – Waiting for changelog lock….导致数据库死锁问题
[NTIRE 2022]Residual Local Feature Network for Efficient Super-Resolution
Analysis on the wallet system architecture of Baidu trading platform
自动化规范检查软件如何发展而来?
Why don't you recommend using products like mongodb to replace time series databases?
Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 -下
Roll up, break through 35 year old anxiety, and animate the CPU to record the function call process
Mysql80 service does not start
移动端异构运算技术-GPU OpenCL编程(进阶篇)
Tianlong Babu TLBB series - single skill group injury