4
- (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. 我想使用上面的代码来重用 UITableViewCell,我不知道它是否正确,我想得到一些建议。
  2. 你可以看到我使用的代码 if(cell==nil),我想知道我应该写什么代码 if(cell!=nil)(if(cell==nil) else{ 我应该做些什么可以提高细胞的重用性})

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

4

3 回答 3

6

1.不正确,因为你的cell是复用的,而且创建的时候不会进去if-statement,所以,在里面if-statement你只需要初始化cell,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:@"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 大多数人的代码是这样的:

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

// setting 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)
        return 20.0f;
    return 40.0f;
}
于 2013-02-27T07:59:53.893 回答
1

我通常将单元的创建和配置分为两个逻辑部分:

  1. 创建单元格并为每个单元格设置相同的所有属性(即布局、层)
  2. 编写一个单独的-(void)configureCell:(UITableViewCell*)cell;函数,在其中设置数据源中特定单元格的所有特定内容(即标签和图像视图的值)。

然后在cellForRowAtIndexPath

-(UITableViewCell *)tableView:(UITableView *)tableView 
        cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"NotificationViewCell";
    CardViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell==nil){
       [self createCell:&cell];
    }

    Mercant* mercantList = [self.cardListArr objectAtIndex:indexPath.row];

    [self configureCell:cell withMercant:mercantList];

    return cell;
}

-(void)createCell:(CardViewCell**)cellPtr
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CardViewCell" 
                                                 owner:self 
                                               options:nil];
    *cellPtr = [nib objectAtIndex:0];

    CardViewCell* cell = *cellPtr;

    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];
}

-(void)configureCell:(CardViewCell*)cell withMercant:(Mercant*)mercantList
{
    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];


    }
}
于 2013-02-27T08:02:38.147 回答
0

Guo Luchuan 的上述答案工作正常,但问题是每次向上和向下滚动时它都会重新创建单元格,如果您的 UIComponents 具有不同的状态,您可能很容易丢失这些状态。此外,我建议您缓存所有创建的单元格并重复使用它们以避免这种情况。

这是缓存如何工作的示例。

var mockCells = [QuestionsCell](repeating: QuestionsCell(), count: 1000)

*在你的 cellForRowAtIndexPath 里面放以下 *

 if cell == indexPath.row {
            //if cell exists in our list we reuse
            cell = mockCell
        } else {
            //here we store/cache each created cell 
                mockCells[indexPath.row] = cell
            }
        }
于 2017-10-05T17:58:33.100 回答