关于iOS中TableVIew(列表)的自定义建立和自定义的Cell

最近研究了一些HTML5的基础,一些C++的基础,有些冷落了个人iOS技术,以致于最近对于iOS有种没有信心的感受,因此今天开始回归个人iOS核心技术,眼前表现为回顾iOS技术,以博客的形式,写总结,好吧,废话很少说atom

 

纯代码形式建立:1.建立tableViewspa

          2.定义一个自定义Cell代理

        3.设置代理orm

        4.代理方法的我实现博客

 

tableView的建立主要有如下步骤:string

1.建立tableView     it

- (void)createTableView
{io

 

//初始化tableView并定义位置,大小。
    UITableView * tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT)];table

//设置table代理的数据源和代理为本身
    tableView.delegate =self;
    tableView.dataSource = self;class

 

//为table 注册自定义的Cell的类。注册方法以下
    [tableView registerClass:[MyCell class] forCellReuseIdentifier:@"MyCell"];

//加入视图。
    [self.view addSubview:tableView];
    
}

 

 

2.自定义Cell


@interface MyCell : UITableViewCell
@property(nonatomic,strong) UILabel * label;
@property(nonatomic,strong) UIImageView * image;

@end

@implementation MyCell

- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    
    self =[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.label  =[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 20, 30)];
        
        self.image  = [[UIImageView alloc]initWithFrame:CGRectMake(20, 30, 50, 50)];
        
        [self.contentView addSubview:self.image];
        [self.contentView addSubview:self.label];
        
    }
    return self;
}

@end

 

3.设置代理

@interface MyTableViewController ()<UITableViewDelegate,UITableViewDataSource>

4.代理方法的实现:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 15;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

//为Cell设置重用的ID
    MyCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
//若是cell没有才建立
    if (cell==nil) {
        cell= [[MyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"];
    }    cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];        return cell;    }

相关文章
相关标签/搜索