UITableViewCell重用良好的做法 [英] UITableViewCell reuse good practice

查看:97
本文介绍了UITableViewCell重用良好的做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


static NSString * CellIdentifier = @ NotificationViewCell;
CardViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){

NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@CardViewCellowner:self options:nil];
cell = [nib objectAtIndex:0];


cell.contentView.backgroundColor = [UIColor clearColor];
[ModelClass addSublayer:cell.contentView];


cell.cellbg.layer.cornerRadius = 8;
cell.cellbg.layer.masksToBounds = YES;

cell.cellbg.layer.borderWidth = 1;
cell.cellbg.layer.borderColor = [[UIColor grayColor] CGColor];


cell.logoImage.layer.cornerRadius = 8;
cell.logoImage.layer.masksToBounds = YES;

cell.logoImage.layer.borderWidth = 1;
cell.logoImage.layer.borderColor = [[UIColor grayColor] CGColor];



商家* merchantList = [self.cardListArr objectAtIndex:indexPath.row];

cell.nameLab.text = merchantList.name;

NSLog(@merchantList.myloyalty.points =%@,merchantList.myloyalty.points);
// NSLog(@memberNO =%@,merchantList.myloyalty.memberNO);
cell.integralLab.text = [NSString stringWithFormat:NSLocalizedString(@points_dot,@),[merchantList.myloyalty.points intValue]];


cell.cardNumberLab.text = [NSString stringWithFormat:@%@%@,NSLocalizedString(@ID,nil),merchantList.myloyalty.memberNO];



if(![ModelClass isBlankString:merchantList.introPic])
{
NSLog(@merchantList.introPic =%@,merchantList .introPic);

[cell.logoImage setImageUrl:merchantList.introPic];


}
}

返回单元格;

}




  1. 我想使用上面的代码重用UITableViewCell,我不知道它是否正确,我想得到一些建议。

  2. 你可以看到我使用的代码if(cell == nil),我想知道我应该写什么代码if(cell!= nil)(if(cell == nil)else {我应该做什么可以改善细胞重用}}


  3. 如果每个单元格都有相同的视图,但有不同的高度,例如imageview有时是20或40,依此类推,如何处理这种情况。



解决方案

1.这是不正确的,因为你的单元格被重用,当它被创建时,它不会进入 if-statement ,因此,在 if-statement 中,你只需要初始化单元格, setText和setImage应该在 if-statement 之外编码。



如:

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


static NSString * CellIdentifier = @NotificationViewCell;
CardViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){

NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@CardViewCellowner:self options:nil];
cell = [nib objectAtIndex:0];


cell.contentView.backgroundColor = [UIColor clearColor];
[ModelClass addSublayer:cell.contentView];


cell.cellbg.layer.cornerRadius = 8;
cell.cellbg.layer.masksToBounds = YES;

cell.cellbg.layer.borderWidth = 1;
cell.cellbg.layer.borderColor = [[UIColor grayColor] CGColor];


cell.logoImage.layer.cornerRadius = 8;
cell.logoImage.layer.masksToBounds = YES;

cell.logoImage.layer.borderWidth = 1;
cell.logoImage.layer.borderColor = [[UIColor grayColor] CGColor];

}

商家* merchantList = [self.cardListArr objectAtIndex:indexPath.row];

cell.nameLab.text = merchantList.name;

NSLog(@merchantList.myloyalty.points =%@,merchantList.myloyalty.points);
// NSLog(@memberNO =%@,merchantList.myloyalty.memberNO);
cell.integralLab.text = [NSString stringWithFormat:NSLocalizedString(@points_dot,@),[merchantList.myloyalty.points intValue]];


cell.cardNumberLab.text = [NSString stringWithFormat:@%@%@,NSLocalizedString(@ID,nil),merchantList.myloyalty.memberNO];



if(![ModelClass isBlankString:merchantList.introPic])
{
NSLog(@merchantList.introPic =%@,merchantList .introPic);

[cell.logoImage setImageUrl:merchantList.introPic];


}


返回单元;

}

2大多数人的代码如下:

  if(cell == nil)
{
// init code
}

//设置代码

3.如果要设置单元格高度,则无法编码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath



你应该dataSource方法中的代码: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath



像这样的代码:

   - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
if(index.row%2)
返回20.0f;
返回40.0f;
}


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


    static NSString *CellIdentifier = @"NotificationViewCell";
    CardViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell==nil){

        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CardViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];


        cell.contentView.backgroundColor=[UIColor clearColor];
        [ModelClass addSublayer:cell.contentView];


        cell.cellbg.layer.cornerRadius = 8;
        cell.cellbg.layer.masksToBounds = YES;

         cell.cellbg.layer.borderWidth = 1;
         cell.cellbg.layer.borderColor = [[UIColor grayColor] CGColor];


        cell.logoImage.layer.cornerRadius = 8;
        cell.logoImage.layer.masksToBounds = YES;

        cell.logoImage.layer.borderWidth = 1;
        cell.logoImage.layer.borderColor = [[UIColor grayColor] CGColor];



        Merchant *merchantList= [self.cardListArr objectAtIndex:indexPath.row];

        cell.nameLab.text=merchantList.name;

        NSLog(@"merchantList.myloyalty.points=%@",merchantList.myloyalty.points);
    //    NSLog(@"memberNO=%@",merchantList.myloyalty.memberNO);
        cell.integralLab.text=[NSString stringWithFormat:NSLocalizedString(@"points_dot", @"") ,[merchantList.myloyalty.points intValue]];


        cell.cardNumberLab.text=[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"ID", nil), merchantList.myloyalty.memberNO];



        if(![ModelClass isBlankString:merchantList.introPic])
        {
              NSLog(@"merchantList.introPic=%@",merchantList.introPic);

            [cell.logoImage setImageUrl:merchantList.introPic];


        }
    }

    return cell;

}

  1. i want to use the above code to reuse UITableViewCell,i donot know if it is correct,i want to get some advice.
  2. you can see my code i use if(cell==nil),i want to know i should write what code if(cell!=nil)(if(cell==nil) else{ i should do what can improve the cell reuse})

  3. if every cell have have same views,but have different height,for example imageview sometimes is 20 or is 40 and so on ,how to deal with the situation.

解决方案

1.It is not correct , because your cell is reused , and when it is created , it will not to go into the if-statement , so , in the if-statement you only need to do is to init the cell , the setText and setImage you should code outside the if-statement .

such as :

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


    static NSString *CellIdentifier = @"NotificationViewCell";
    CardViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell==nil){

        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CardViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];


        cell.contentView.backgroundColor=[UIColor clearColor];
        [ModelClass addSublayer:cell.contentView];


        cell.cellbg.layer.cornerRadius = 8;
        cell.cellbg.layer.masksToBounds = YES;

         cell.cellbg.layer.borderWidth = 1;
         cell.cellbg.layer.borderColor = [[UIColor grayColor] CGColor];


        cell.logoImage.layer.cornerRadius = 8;
        cell.logoImage.layer.masksToBounds = YES;

        cell.logoImage.layer.borderWidth = 1;
        cell.logoImage.layer.borderColor = [[UIColor grayColor] CGColor];

        }

        Merchant *merchantList= [self.cardListArr objectAtIndex:indexPath.row];

        cell.nameLab.text=merchantList.name;

        NSLog(@"merchantList.myloyalty.points=%@",merchantList.myloyalty.points);
    //    NSLog(@"memberNO=%@",merchantList.myloyalty.memberNO);
        cell.integralLab.text=[NSString stringWithFormat:NSLocalizedString(@"points_dot", @"") ,[merchantList.myloyalty.points intValue]];


        cell.cardNumberLab.text=[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"ID", nil), merchantList.myloyalty.memberNO];



        if(![ModelClass isBlankString:merchantList.introPic])
        {
              NSLog(@"merchantList.introPic=%@",merchantList.introPic);

            [cell.logoImage setImageUrl:merchantList.introPic];


        }


        return cell;

    }

2 Most people code like this :

if(cell==nil)
{
    //init code
}

// setting code

3.If you want set the cell height , you can not code in the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

you should code in the method of dataSource : - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

code like this :

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (index.row % 2)
        return 20.0f;
    return 40.0f;
}

这篇关于UITableViewCell重用良好的做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆