iOS 代碼自訂cell樣本

來源:互聯網
上載者:User

iOS 代碼自訂cell樣本

底色標黃為代碼自訂cell重點處,入手從這幾點即可。

 

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 =@點擊;

[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


控制器.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) {//如果想修改字典中的值,字典必須為可變字典不然就會出現問題

 

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



 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.