IOS Programming subclassing UITableViewCell?
1.Creating Bnritemcell
UITableViewCell is a UIView subclass.
UITableViewCell is a subclass of UIView.
When subclassing UIView (or any of it subclasses), you often override it drawrect:method to customize the view ' s appear Ance.
When inheriting UIView, you often need to rewrite the DrawRect method to accommodate the appearance of the view.
However, when subclassing UITableViewCell, you usually customize it appearance by adding subviews to the cell.
When inheriting UITableViewCell, you often adapt to its appearance by adding subviews to the cell.
You don't add them directly to the cell though; Instead you add them to the cell ' s content view.
You add them to the cell's content view.
Each cell have a subview named Contentview, which is a container for the view objects so make up the layout of a cell sub Class
Adding Subviews to the Contentview instead of directly to the cell itself is important because the cell would resize its co Ntentview at certain times.
It is important to add subview to the content view instead of directly to the cell, because the cell will resize its content view at a specific time.
For example, when a table view enters editing mode the Contentview resizes itself to make the the editing controls
When table view enters editing mode, Contentview resizes them to make room for edit control.
The cell cannot adjust its size is entering edit mode (it must remain the width of the table view), but the Contentview Can resize, and it does.
1.1
By the the-the-notice the Uiscrollview in the cell hierarchy? That's how IOS moves the contents of the cell to the left when it enters editing mode. You can also use a right-to-left swipe in a cell to show the delete control, and this uses that same scroll view to get th E job done. It makes sense then, the Contentview is a subview of the scroll view.
Notice that there is a uiscrollview in cell hierarchy?
This is why the content of the cell moves to the left when it enters edit mode.
Open homepwner.xcodeproj. Create a new NSObject subclass and name it Bnritemcell. In BNRItemCell.h, change the superclass to UITableViewCell.
@interface Bnritemcell:uitableviewcell
2.? Configuring a UITableViewCell subclass ' s interface
The easiest-Configure a UITableViewCell subclass is with a XIB file. Create a new Empty XIB
File and name this file bnritemcell.xib. (The Device Family is irrelevant for this file.)
The simplest way is to use a xib file.
This file would contain a single instance of Bnritemcell. When the table view needs a new cell, it would
Create one from this XIB file.
?
In Bnritemcell.xib, select Bnritemcell.xib and drag a UITableViewCell instance from the object library to the canvas.
?
Select the Table view Cell in the outline view and then the Identity Inspector (The?tab). Change the Class to Bnritemcell
Double-check that BNRItemCell.h looks like this: @interface Bnritemcell:uitableviewcell
@property (Weak, nonatomic) Iboutlet Uiimageview *thumbnailview; @property (Weak, nonatomic) Iboutlet UILabel *namelabel; @property (Weak, nonatomic) Iboutlet UILabel *serialnumberlabel; @property (Weak, nonatomic) Iboutlet UILabel *valuelabel;
@end
?
Note that you do not specify the File's Owner class or make any connections with it. This was a little different than your usual XIB files where all of the connections happen between the File's Owner and the Archived objects.
You did not specify the file ' Owner class and other links. This is a bit different.
3. Using Bnritemcell
In Bnritemsviewcontroller ' s TableView:cellForRowAtIndexPath:method, you'll create an
Instance of Bnritemcell for every row in the table.
In bnritemsviewcontroller.m, import the header file for Bnritemcell so
Bnritemsviewcontroller knows about it.
#import "BNRItemCell.h"
Now it is the using a custom NIB file to the load a UITableViewCell subclass, you'll register that nib instead.
Now that you have loaded the UITableViewCell subclass with a generic nib file, you will register this nib.
In bnritemsviewcontroller.m, modify viewdidload to register bnritemcell.xib for the "Bnritemcell" reuse identifier.
-(void) viewdidload
{
[Super Viewdidload];
?
Load the NIB file
uinib *nib = [uinib nibwithnibname:@ "Bnritemcell" bundle:nil];
Register this NIB, which contains the cell
[Self.tableview registernib:nib
forcellreuseidentifier:@ "Bnritemcell"];
}
The registration of a NIB for a table view isn't anything fancy:the table view simply stores the uinib instance in an NS Dictionary for the key "Bnritemcell".
The registration of a nib file is not magical: Table View simply stores the uinib instance to a nsdictionary,key that is Bnritemcell.
A uinib contains all of the data stored in its XIB file, and when asked, can create new instances of the objects it Contai Ns.
A uinib contains all the data stored in the Xib file and, when requested, can create a new object instance that it contains.
Once a uinib have been registered with a table view, the table view can is asked to load the instance of Bnritemcell when G Iven the reuse identifier "Bnritemcell".
Once a uinib is registered in a table view, the table view can be asked to load an instance of Bnritemcell.
In bnritemsviewcontroller.m, modify Tableview:cellforrowatindexpath:.
Get a new or recycled cell Bnritemcell *cell =
[TableView dequeuereusablecellwithidentifier:@ "Bnritemcell" Forindexpath:indexpath];
Configure the cell with the Bnritem
Cell.nameLabel.text = Item.itemname;
Cell.serialNumberLabel.text = Item.serialnumber;
Cell.valueLabel.text =
[NSString stringwithformat:@ "$%d", Item.valueindollars];
?
Bnritemsviewcontroller ' s table view would change its size to match the size of the window.
When a table view changes it width, each of its cells also the change their the width to match. Thus, you need-to set-constraints in the cell-that's the-the-in width.
When the table view changes its width, its cell will change their width to match. Therefore, if you need to set a limit for the width change. (the cell's height will not change unless you explicitly point it out.) )
However, Auto Layout does not care about how big a view was when it was first created; It's only cares on what the constraints say.
Auto layout doesn't care how big the view you created for the first time. It cares only about what constraints said.
If you were to add a constraint to the image view this pins its width to $ points, the width would be points–the o Riginal size does not factor in.
IOS Programming subclassing UITableViewCell