IOS code custom cell example
The base color and yellow color are the key points for custom cell codes. Start with these points.
MyCell. h
# Import
@ Interface MyCell: UITableViewCell
@ Property (nonatomic, strong) UILabel * ageLabel;
@ Property (nonatomic, strong) UILabel * nameLabel;
@ Property (nonatomic, strong) UILabel * additionLabel;
-(Instancetype) initWithStyle :( UITableViewCellStyle) style reuseIdentifier :( NSString *) reuseIdentifier;
@ End
MyCell. m
# Import MyCell. h
@ Implementation MyCell
-(Instancetype) initWithStyle :( UITableViewCellStyle) style reuseIdentifier :( NSString *) reuseIdentifier
{
If (self = [superinitWithStyle: stylereuseIdentifier: reuseIdentifier]) {
// For (UIView * view in self. contentView. subviews ){
// NSLog (@ % @, view );
// [View removeFromSuperview];
//}
_ AgeLabel = [[UILabelalloc] init];
_ NameLabel = [[UILabelalloc] init];
_ AdditionLabel = [[UILabelalloc] init];
_ AdditionLabel. text = @ click;
[Self. contentViewaddSubview: _ ageLabel];
[Self. contentViewaddSubview: _ nameLabel];
[Self. contentViewaddSubview: _ additionLabel];
Self. backgroundColor = [UIColorclearColor];
}
Return self;
}
-(Void) layoutSubviews
{
[SuperlayoutSubviews]; // if you do not call the parent class, this method will lead to incorrect separation line position during use, mainly because the parent class will call its separation line position adjustment.
Self. nameLabel. frame = CGRectMake );
Self. ageLabel. frame = CGRectMake (100,0, 100,44 );
Self. additionLabel. frame = CGRectMake (320, 20 );
}
@ End
Controller. m
# Import ViewController. h
# Import ConcernViewController. h
# Import MyCell. h
@ Interface ViewController ()
@ Property (nonatomic, strong) UITableView * myTableView;
@ Property (nonatomic, strong) NSMutableArray * myData;
@ End
@ Implementation ViewController
-(NSMutableArray *) myData
{
If (! _ MyData) {// If You Want To modify the value in the dictionary, the dictionary must be a variable dictionary. Otherwise, the problem may occur.
NSMutableDictionary * dict1 = [NSMutableDictionarydictionaryWithObjects: @ [@ hua, @ 12, @ NO] forKeys: @ [@ name, @ age, @ flag];
NSMutableDictionary * dict2 = [NSMutableDictionarydictionaryWithObjects: @ [@ huhu, @ 14, @ NO] forKeys: @ [@ name, @ age, @ flag];
NSMutableDictionary * dict3 = [NSMutableDictionarydictionaryWithObjects: @ [@ huxun, @ 16, @ YES] forKeys: @ [@ name, @ age, @ flag];
_ MyData = [NSMutableArray arrayWithObjects: dict1, dict2, dict3, nil];
}
Return_myData;
}
-(Void) viewDidLoad {
[SuperviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CGFloat customW = self. view. bounds. size. width;
CGFloat customH = self. view. bounds. size. height;
UITableView * tableview = [[UITableView alloc] initWithFrame: CGRectMake (10, 44, customW-2*10, customH-80) style: UITableViewStylePlain];
Tableview. delegate = self;
Tableview. dataSource = self;
// Tableview. separatorStyle = UITableViewCellSeparatorStyleNone;
Tableview. separatorColor = [UIColorblueColor];
Tableview. separatorInset = UIEdgeInsetsMake (0, 20, 0, 20 );
Self. myTableView = tableview;
[Self. viewaddSubview: self. myTableView];
}
-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView
{
Return 1;
}
/**
* Returns the number of rows in each group.
*
*/
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section
{
Return self. myData. count;
}
/**
* Return cell
*
*/
-(MyCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath
{
StaticNSString * ID = @ MyCell;
MyCell * cell = [tableViewdequeueReusableCellWithIdentifier: ID]; // cell Reuse
If (cell = nil ){
Cell = [[MyCellalloc] initWithStyle: UITableViewCellStyleDefaultreuseIdentifier: ID];
}
// Cell value Initialization
Return cell;
}
-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath;
{
If ([self. myData [indexPath. row] [@ flag] boolValue]) {
Return 44 + 30;
}
Return 44;
}
-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath
{
}
-(Void) didReceiveMemoryWarning {
[SuperdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@ End