UITableViewis one of the most important UserInterface in iOS. It's not difficult to implement UITableView, almost every app uses its functionality. If one wants to customize and add additional features to tableview cell, one might need to create Custom TableViewCell Class.
Here is the end result what we are going to build.
Creating an UITableView
There are two ways of creating UITableView, either you can use it programmatically or it can be done using interface builder. The second approach is the most common one, so will do in this way.
Step 1 - Create XCode Project
Open Xcode and create a new project by selecting Single View App from XCode IDE, and name the project as customTableView
Step 2 - Create a Model
Click File from Menu, and select Swift file i.e., File -> New -> File -> Swift File and name the file as CustomTableData.
We create this model to fetch the data into your tableview. Model consisting of properties which are going to be used in our custom-tableview-cell. After creating the CustomTableData file add the below code in the newly created swift file.
class CustomTableData {
var imageName: String?
var titleName: String?
init(imageName:String?, titleName: String?) {
self.imageName = imageName
self.titleName = titleName
}
}
Step 3 - Create CustomTableViewCell
Next step is to create a CustomTableViewCell including xib. Click File from Menu, and select Cocoa Touch Class i.e., File -> New -> File -> Cocoa Touch Class and name the file as CustomTableViewCell and UITableViewCell as it subclass
Tick "Also Create XIB file" option, which automatically creates XIB File to your TableViewCell
Add UIImageView and UILabel Outlets in the CustomTableViewCell Class
@IBOutletweakvar leftImage: UIImageView!
@IBOutletweakvar titleLabel: UILabel!
Click on CustomTableViewCell.xib to set outlets for image and label and a suitable name to register cell identifier
CustomTableCellIdentifier is the name of this identifier
Finally the CustomTableViewCell class implementation looks like below
Open Main.storyboard, drag and drop UITableView to the ViewController from Library and set the customTableView Outlet to the UITableView as shown below
Next Create a function which loads the data to the CustomTableData, and call this function from ViewDidLoad
Simple and easy explanation
ReplyDelete