UITableViewCell reuse good practice

后端 未结 3 806
天命终不由人
天命终不由人 2021-01-13 17:41
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    static NSString *CellIdentifier = @\"NotificationViewC         


        
3条回答
  •  被撕碎了的回忆
    2021-01-13 18:07

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

提交回复
热议问题