如何快速启动tableview

Hi everyone! Today I am going to show you step by step guide for “How to start with TableView in Swift?”. If you are new to swift and want to know that what are the various steps which you should follow in the initial phase of learning then you can go and check my blog “Beginners Guide for Swift Programming Language”. So without wasting any time let’s start with TableView in swift.

嗨,大家好! 今天,我将向您展示“如何从Swift中的TableView开始?”的分步指南。 如果您不熟悉Swift ,并且想知道在学习的初始阶段应该遵循哪些步骤,那么可以去阅读我的博客“ Swift编程语言入门指南 ”。 因此,不浪费时间,让我们快速开始使用TableView。

What is TableView?

什么是TableView?

TableView is an instance of UITableView which is itself a subclass of UIScrollView. A table view contains a list of TableView cells. A table view cell is an instance of UITableViewCell.

TableView是UITableView的实例,它本身是UIScrollView的子类。 表格视图包含TableView单元格的列表。 表格视图单元格是UITableViewCell的实例。

Where to use Tableview?

在哪里使用Tableview?

A table view is used to show the list of items. For example, if we look at the iPhone’s contacts list, settings page, or wheresoever the list of items is shown. Generally, we use table view if we want our page to be scrolled vertically for horizontal scrolling we can use CollectionView.

表格视图用于显示项目列表。 例如,如果我们查看iPhone的联系人列表,设置页面或显示项目列表的任何位置。 通常,如果希望页面垂直滚动以进行水平滚动,则可以使用表格视图,可以使用CollectionView。

Steps to add TableView using Swift:

使用Swift添加TableView的步骤:

  1. Go to the main.storyboard and the View controller.

    转到main.storyboard和View控制器。

  2. Now Drag Tableview from Library option to the View controller.

    现在将Tableview从Library选项拖到View控制器。
Use library in swift

3. Add constraints according to the need.

3.根据需要添加约束。

4. Now Drag and Drop Table View Cell on to the TableView.

4.现在将Table View Cell拖放到TableView上。

Add cell in storyboard

5. Add a class for tale view cell from cocoa touch class having subclass UITableViewCell.

5.从具有子类UITableViewCell的可可触摸类中为故事视图单元格添加类。

Add class in swift

6. Now select TableView cell from the storyboard and give a class name to the cell in an identity inspector.

6.现在,从情节提要中选择TableView单元,并在身份检查器中为该单元指定一个类名。

Custom class on storyboard in swift

7. Now Go to Attribute inspector and give a unique identifier to the cell.

7.现在转到“属性”检查器,并为单元格提供一个唯一的标识符。

Add identifier in table view using swift

8. Now add delegate and data source methods to the table view.

8.现在,将委托和数据源方法添加到表视图中。

Delegate and Data Source in swift

Note: Datasource methods used to display the data on the table view cell and the Delegate methods used to give information about the cells.

注意:用于在表视图单元格上显示数据的数据源方法和用于提供有关单元格信息的Delegate方法。

9. Give the outlet to the table view and connect it with the view controller.

9.将插座提供给表格视图,并将其与视图控制器连接。

Now try to run your code you will be able to see a blank list with an infinite number of cells.

现在尝试运行您的代码,您将能够看到包含无限数量单元格的空白列表。

Table view in swift

10. Now let’s come to the coding part. As you have already connected the delegate and data source with the table view now comes the part of using these properties. Inherit the UITableViewDelegate and UITableViewDataSource into the table view. Now you have access to all the delegate and data source methods. You can use those methods according to your need.

10.现在让我们进入编码部分。 由于已经将委托和数据源与表视图连接起来,因此现在是使用这些属性的一部分。 将UITableViewDelegateUITableViewDataSource继承到表视图中。 现在,您可以访问所有委托和数据源方法。 您可以根据需要使用这些方法。

11. There are two methods which are mandatory to add below are these methods :

11.以下是必须添加的两种方法:

  • tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int

    tableView(_ tableView:UITableView,numberOfRowsInSection部分:Int)-> Int

The above method defines the total number of rows that need to show in the table view.

上面的方法定义了需要在表视图中显示的总行数。

  • tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

    tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)-> UITableViewCell

The above method used to design your cell. This method called for each cell and the cell number is found by indexPath. Here you can use the Label, textField, imageView, Button, and other UI elements.

以上方法用于设计您的单元格。 该方法为每个单元格调用,单元格编号由indexPath查找。 在这里,您可以使用LabeltextFieldimageViewButton和其他UI元素。

12. Create a cell using the dequeueReusableCell property of table view which helps to reuse the same cell multiple times and give a unique identifier as a parameter which we set in the Attribute inspector. Use “as” property to check whether it is matching with the class of cells.

12.使用表视图的dequeueReusableCell属性创建一个单元格, 属性有助于多次重用同一单元格,并提供一个唯一的标识符作为参数,该参数是我们在属性检查器中设置的。 使用“ as”属性检查它是否与单元格的类别匹配。

13. Now you can give labels to the cell and add text into it. Below is the final code which shows the 20 cells with cell number.

13.现在,您可以给单元格添加标签,并在其中添加文本。 下面是最终代码,显示了具有单元号的20个单元。

Cell creation in swift

Now run your application. It will look like the below-attached screenshot.

现在运行您的应用程序。 它看起来像下面所附的屏幕截图。

How use table view in swift

That’s all you have successfully created an application using a table view.

这就是您使用表视图成功创建应用程序的全部。

结论 (Conclusion)

A Tableview is an asset for UI development in the iOS application. It has many properties. You just need to google more about it and it’s all up to you how much effort you will put to get the best out of it.

Tableview是用于iOS应用程序中的UI开发的资产。 它具有许多属性。 您只需要在Google上搜索更多有关它,您就将全力以赴,以充分利用它。

Please do share your valuable comment in the comment box given below and do write any topic which you think should be there in the above list.

请在下面的评论框中分享您的宝贵评论,并在上面的列表中写下您认为应该存在的任何主题。

Thank You

谢谢

翻译自: https://medium.com/@sahilpathania1997/how-to-start-with-tableview-in-swift-bf273a8bbabe

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值