你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS 代碼自定義cell示例

iOS 代碼自定義cell示例

編輯:IOS開發綜合

底色標黃為代碼自定義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



 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved