UIKit provides default renderers for the core data types: text, image, and file. It also provides a way to render your own UI for your custom post.
Post Structure:
A single post UI is composed of 1 or more UITableViewCell. This allows reusability of cell components such as header or footer which can be shared by different kinds of posts.
This is an example of simple text post which contains 3 tableview cell. Top cell represents the header, middle cell contains text content and bottom cell is footer cell.
Creating UI for custom posts :
There are 3 steps involved in creating custom post renderer.
Step 2: Compose a post UI component. For this create a component which conforms to AmityPostComposable protocol.
structMyCustomPostComponent:AmityPostComposable {var post: AmityPostModel// Post data model which you can use to render ui.init(post: AmityPostModel) { self.post = post }funcgetComponentCount(forindex: Int) ->Int {return2 }funcgetComponentCell(_tableView: UITableView, atindexPath: IndexPath) -> UITableViewCell {switch indexPath.row {case0: let cell = tableView.dequeueReusableCell(withIdentifier: "my-cell-identifier", for: indexPath) as! MyPostHeaderCell
// ... populate cell data herereturn cellcase1: let cell = tableView.dequeueReusableCell(withIdentifier: "my-cell-identifier", for: indexPath) as! MyPostContentCell
// ... populate cell data herereturn celldefault:fatalError("indexPath is out of bound") } }// Height for each cell componentfuncgetComponentHeight(indexPath: IndexPath) -> CGFloat {return UITableView.automaticDimension }}
You can also use our default header cell AmityPostHeaderTableViewCell & default footer cell AmityPostFooterTableViewCell in your custom post component.
Step 3: Register your custom cell & implement datasource.
classYourViewController:UIViewController {funcshowFeed() {// 1.// Register your cell here. You can register both // nib as well as class itself. AmityFeedUISettings.shared.register(UINib(nibName: "PostThumbsupTableViewCell", bundle: nil), forCellReuseIdentifier: "PostThumbsupTableViewCell")
AmityFeedUISettings.shared.dataSource = self// Showing our feed UIlet feedViewController = AmityGlobalFeedViewController.make() navigationController?.pushViewController(feedViewController, animated:true) }}extensionYourViewController:AmityFeedDataSource {// 2. // Provide your rendering component for custom post.funcgetUIComponentForPost(post: AmityPostModel, atindex: Int) -> AmityPostComposable? {switch post.dataType {case"your-data-type":returnMyCustomPostComponent(post: post)default:returnnil } }}