Last active
June 20, 2018 06:33
-
-
Save lamprosg/e719bb3cb8beaca35427 to your computer and use it in GitHub Desktop.
(iOS) Reusable cell with separate xib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /************************/ | |
| //Optional Reference link | |
| //https://medium.com/@musawiralishah/creating-custom-uitableviewcell-using-nib-xib-files-in-xcode-9bee5824e722 | |
| /************************/ | |
| //Add an empty view to your project | |
| //New file -> iOS / User Interface -> Empty | |
| //Drag a UITableViewCell onto it and fix it how you want | |
| //Make a custom UITableViewCell class | |
| //Set the custo class and a reuse identifier to the xib cell | |
| //Register the nibs in viewDidLoad for the tableView | |
| //Ex. in a TableViewController. | |
| - (void)viewDidLoad | |
| { | |
| [super viewDidLoad]; | |
| self.tableView.backgroundView = nil; | |
| [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; | |
| // Load cells | |
| UINib *nib1 = [UINib nibWithNibName:<NameOfTheXib> bundle:nil]; | |
| [self.tableView registerNib:nib0 forCellReuseIdentifier:<Xibs reuse identifier 1>]; | |
| UINib *nib2 = [UINib nibWithNibName:<NameOfTheXib> bundle:nil]; | |
| [self.tableView registerNib:nib1 forCellReuseIdentifier:<Xibs reuse identifier 2>]; | |
| } | |
| //TableView datasource | |
| - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
| { | |
| MyCustomCell* cell = [tableView dequeueReusableCellWithIdentifier:<Xibs reuse identifier 1>]; | |
| [cell setSomeData:data]; | |
| return cell; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| /*****************/ | |
| //Register nibs | |
| let loaderCellNib:UINib = UINib(nibName:"theNibName", bundle: nil) | |
| self.tableView.register(loaderCellNib, forCellReuseIdentifier:"theNibNameID") | |
| /*****************/ | |
| } | |
| //Table View datasource (Override if parent is UITableViewController | |
| func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
| let cell:MessageCell = self.tableView.dequeueReusableCell(withIdentifier: kmessageCellIdentifier) as! MessageCell | |
| cell.selectionStyle = UITableViewCellSelectionStyle.none | |
| cell.setMessage(NSLocalizedString("Report", comment: "Report item"), textColor: Palette.uiColorFromHex(Palette.reportItemColor), font:Palette.standardFontBold) | |
| return cell | |
| } | |
| // MARK: - Table view data source | |
| func numberOfSections(in tableView: UITableView) -> Int { | |
| return 1 | |
| } | |
| func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
| return self.itemViewModel.numberOfCells | |
| } | |
| func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { | |
| return 0.0 | |
| } | |
| func tableView( _ tableView : UITableView, titleForHeaderInSection section: Int) -> String? { | |
| return "" | |
| } | |
| func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { | |
| return self.height(viewType:self.itemViewModel.viewType(index:indexPath.row)) | |
| } | |
| func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { | |
| return self.height(viewType:self.itemViewModel.viewType(index:indexPath.row)) | |
| } | |
| // MARK: - Table view delegate | |
| func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | |
| } |
Author
lamprosg
commented
Jun 20, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment