底色标黄为代码自定义cell重点处,入手从这几点即可。
MyCell.h
#import <UIKit/UIKit.h>
@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 =@"点击";
[self.contentViewaddSubview:_ageLabel];
[self.contentViewaddSubview:_nameLabel];
[self.contentViewaddSubview:_additionLabel];
self.backgroundColor = [UIColorclearColor];
}
return self;
}
- (void)layoutSubviews
{
[superlayoutSubviews]; //如不调用父类此方法就会导致在使用时出现分隔线位置不正确,主要是因为父类中会调用其分隔线位置调整
self.nameLabel.frame =CGRectMake(0,0,100,44);
self.ageLabel.frame =CGRectMake(100,0,100,44);
self.additionLabel.frame =CGRectMake(0,40,320, 20);
}
@end
#import "ViewController.h"
#import "ConcernViewController.h"
#import "MyCell.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,ConcernViewContorllDelegate>
@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,strong)NSMutableArray *myData;
@end
@implementation ViewController
- (NSMutableArray *)myData
{
if (!_myData) {//如果想修改字典中的值,字典必须为可变字典不然就会出现问题
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;
}
/**
* 返回每组有多少行
*
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.myData.count;
}
/**
* 返回cell
*
*/
- (MyCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
staticNSString *ID = @"MyCell";
MyCell *cell = [tableViewdequeueReusableCellWithIdentifier:ID]; // cell的重用
if (cell ==nil) {
cell = [[MyCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:ID];
}
// cell值的初始化
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
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/linfengwenyou/article/details/47192405