UITableView的组的自定义与cell的重用

《一》 UITableVIewCell的一个属性

@property (nonatomic, readonly, copy) NSString      *reuseIdentifier;

 所以在设置UITableVIewCell时候都要设置每种cell的Identifier标志来取cell,且每次创建的cell 都放放到dequue的数组里来管理cell的重用,

1.判断首先进来的是哪个组,用参数 indexPath.section获取到所取得得数组

2.首先在dequue的数组里用标志来取cell,实现重用   cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];

3.如果cell 为空, 则cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell1"];来创建;

4.然后index.Path.row来自定义cell的大小,以及在cell上加子视图,(子视图要加到cell.content.view上

5.- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 来设置section的title


- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view.

    _homeTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width / 3, self.view.frame.size.height)];

    _homeTableView.delegate = self;

    _homeTableView.dataSource = self;

    [_homeTableView setBackgroundColor:[UIColor orangeColor]];

    _homeTableView.separatorColor = [UIColor purpleColor];

    

   // _homeTableView.

    //

    

    [self.view addSubview:_homeTableView];

}


//tabelView DataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    NSInteger num = 0;

    if (section == 0) {

        num = 1;

    }

    else if (section == 1) {

        num = 4;

    }

    else if (section == 2) {

        num = 2;

    }

        

    return num;

}


// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:

// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)


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

{

    

    

    UITableViewCell  *cell = nil;

    if (indexPath.section == 0) {

        cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];

        if (nil == cell) {

            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell1"];

            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(6, 6, 35, 35)];

            imageView.image = [UIImage imageNamed:@"Dot.png"];

            //contentView

            [cell.contentView addSubview:imageView];

        }

        

        

    }

    else if (indexPath.section == 1) {

        cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];

        if (nil == cell) {

            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell2"];

        }

        switch (indexPath.row) {

            case 0:

                cell.textLabel.text = @"全部";

                break;

            case 1:

                cell.textLabel.text = @"文档";

                break;

            case 2:

                cell.textLabel.text = @"图片";

                break;

            case 3:

                cell.textLabel.text = @"音频";

                break;

            default:

                break;

        }

    }

    

    else if (indexPath.section == 2) {

        cell = [tableView dequeueReusableCellWithIdentifier:@"cell3"];

        if (nil == cell) {

            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell3"];

        }

        switch (indexPath.row) {

            case 0:

                cell.textLabel.text = @"本地相册";

                break;

            case 1:

                cell.textLabel.text = @"网盘相册";

                break;

            default:

                break;

        }

    }

    cell.backgroundColor = [UIColor clearColor];

    return cell;

    

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 3;

}



- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

    NSString *title = nil;

    if (section == 0) {

        title = @"SugarPhoto";

    }

    if (section == 1) {

        title = @"我的文件";

    }

    if (section == 2) {

        title = @"文件来源";

    }

    return title;

}


//uitableView Delegate


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    CGFloat  key;

    if (indexPath.section == 0) {

        key = 50;

    }

    if (indexPath.section == 1 || indexPath.section == 2) {

        key = 44;

    }

    return key;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

    return 35;

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值