Hope you have enjoyed my previous tutorial if not ,go and explore it here
This second part of UITableView tutorial is all about customizing Table View using xib.Creating your own tableview cell could be such a hassle when you do it programmatically. To be able to follow this tutorial, you need to know how to add a tableview first. Refer to the previous tutorial on how to add a tableview if you still don’t know how to add it yet.
1. Add a new file of subclass UITableViewCell.
2. After adding a new UITableViewCell class, only .h and .m files are created. So, let’s add another file, an empty Interface Builder Document (xib file). Name it with the same name with our .h and .m files (to avoid confusion).
3. Drag a tableview cell to your xib file, and change its class to SampleTableCell (the one we added a while ago).
4. Start customizing your cell. Here in my example, I want to make a Top Artist Cell (One with profile pictures, & name)
- I changed the height of my cell to 90 (instead of the default cell height which is 44).
- I added a UIImageView for the profile pictures.
- Added a UILabel for the name.
5. Make an IBOutlet property of the objects that might change dynamically. In our example, our profilePicture object may change dynamically, depends on who is on Top Artist Chart. And remember to synthesize these objects.
//Inside .h file
//Inside .m file
7. Create a new File, subclass of NSObject. This file will serve as our “Artist” object. This class must hold the important information we need, like the profilePicture filename, Name.
8. Create your Artist objects and put them all in your items Array.
//I created five Artist. Sample code…
//Put them all in our array list
items = [[NSArray alloc] initWithObjects: artist1, artist2, artist3, artist4, artist5, nil];
9. Before we proceed, change the cell’s Identifier . We need it later.One more thing add some Artist images in project
Remember the Datasource and Delegate methods of our tableView from the previous tutorial? We are going to modify/add some methods.
10. Remember that our customized cell’s height was changed to 90, right? It has become taller than the usual height of our tableViewCell. Add this method so that our tableView will know that the cell’s height was changed.
//If we did not change the height of our cell, we do not need to add this method.
11. Lastly, modify this method cellForRow. This is the part where we tell the tableView that we will use our custom cell.
Artist *artist = [items objectAtIndex:indexPath.row];
cell.picture.image= [UIImage imageNamed:artist.imageName];
cell.name.text = artist.artistName;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
12. Hit Run! check output as shown below