Skip to content

Instantly share code, notes, and snippets.

@lamprosg
Last active June 20, 2018 06:33
Show Gist options
  • Select an option

  • Save lamprosg/e719bb3cb8beaca35427 to your computer and use it in GitHub Desktop.

Select an option

Save lamprosg/e719bb3cb8beaca35427 to your computer and use it in GitHub Desktop.
(iOS) Reusable cell with separate xib
/************************/
//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;
}
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) {
}
@lamprosg
Copy link
Author

       var cell:UITableViewCell? = self.tableView.dequeueReusableCell(withIdentifier: kdefaultDetailedCellIdentifier)
        
        if (cell == nil) {
            cell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier:kdefaultDetailedCellIdentifier)
            
            cell!.selectionStyle = UITableViewCellSelectionStyle.none
            
            cell!.textLabel?.textColor = Palette.uiColorFromHex(Palette.cellTitleHexColor)
            cell!.detailTextLabel?.textColor = Palette.uiColorFromHex(Palette.cellDetailHexColor)
            
            cell!.textLabel?.font = Palette.standardFont
            cell!.detailTextLabel?.font = Palette.standardFont
        }
        return cell!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment