How To Create TableView
-
Open Main.storyboard: In the project navigator, double-click on the Main.storyboard file to open the Interface Builder.
-
Add a UITableView: From the Object Library (bottom-right panel), search for “Table View” and drag and drop it onto the canvas of the view controller.
-
Configure the UITableView: Select the table view on the canvas, and in the Attributes Inspector (right-side panel), customize the appearance and behavior of the table view. Set the number of prototype cells, adjust row height, and enable any desired features such as section headers or footers.
-
Create a UITableViewCell prototype: Select the table view on the canvas, and in the Attributes Inspector, set the “Prototype Cells” value to the desired number of cells. Customize each prototype cell by dragging UI elements (labels, images, etc.) onto the cell.
-
Configure UITableView data source and delegate: Open the Assistant Editor by clicking the icon in the top-right corner of Xcode’s editor. Make sure the storyboard is visible in the left pane, and the view controller’s code is visible in the right pane. Ctrl-drag from the table view to the view controller’s code to create an outlet for the table view. Also, make the view controller conform to the
UITableViewDataSource
and UITableViewDelegate protocols. -
Implement
UITableViewDataSource
methods: In the view controller’s code, implement the required methods of theUITableViewDataSource
protocol to provide data to the table view. These methods includenumberOfSections
(in:),tableView
(:numberOfRowsInSection:), andtableView
(:cellForRowAt:). Return the appropriate values and configure the cells with the desired data. -
Implement
UITableViewDelegate
methods (optional): If you need to handle user interactions with the table view, implement the relevant methods of theUITableViewDelegate
protocol. These methods includetableView
(:didSelectRowAt:),tableView
(:heightForRowAt:), andtableView
(_:viewForHeaderInSection:), among others. -
Build and run the project: Press Cmd+R or click the “Run” button in Xcode’s toolbar to build and run the project in the iOS Simulator or a connected device. You should see your table view with the configured cells.