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

来源:互联网 发布:湖南软件职业学院招聘 编辑:程序博客网 时间:2024/06/08 00:59
《一》 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

{

    [superviewDidLoad];

// Do any additional setup after loading the view.

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

    _homeTableView.delegate =self;

    _homeTableView.dataSource =self;

    [_homeTableView setBackgroundColor:[UIColororangeColor]];

    _homeTableView.separatorColor = [UIColorpurpleColor];

    

   // _homeTableView.

    //

    

    [self.viewaddSubview:_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 = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"cell1"];

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

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

            //contentView

            [cell.contentView addSubview:imageView];

        }

        

        

    }

    else if (indexPath.section ==1) {

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

        if (nil == cell) {

            cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"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 = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"cell3"];

        }

        switch (indexPath.row) {

            case 0:

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

                break;

            case 1:

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

                break;

            default:

                break;

        }

    }

    cell.backgroundColor = [UIColorclearColor];

    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;

}