zoukankan      html  css  js  c++  java
  • iOS开发之NSBundle加载自定义cell需指定其的identifier

    【参考自】http://blog.csdn.net/yohunl/article/details/19619167

     在写cellForRowAtIndexPath方法的时候,经常会使用自己定义的cell,然后使用NSBundle的方式来加载。但是经常因为没有设置cell的reuseIdentifier属性而让cell并没有重用,但是,开发者有时候也是不一定知道的。所以,这个地方需要注意:

    有两种方法可以再使用NSBundle的方式创建Cell的时候指定cell的identifier。

    【1】代码方式:

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        NSInteger row = indexPath.row;
        static NSString *cellString = @"CommonTableViewCell";
        CommonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellString];
        if (cell == nil) {
            NSArray *nibArr = [[NSBundle mainBundle]loadNibNamed:cellString owner:self options:nil];
            cell = (CommonTableViewCell *)[nibArr firstObject];
            [cell setValue:cellString forKey:@"reuseIdentifier"];
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        
        cell.title.text = [tempDict objectForKey:@"title"];
        cell.content.text = [tempDict objectForKey:@"content"];
        
        return cell;
    }
    

    【2】使用xib文件的属性配置面板添加配置。如下图所示:


    只有指定了这样的两种方式,创建的cell才是重用的,如果没有指定创建的cell的identifier,cell是不会重用的,亲测。

    网上很多人关于NSBundle创建cell的写法都没有提到半个字需要指定identifier值的问题。。。有点坑~

  • 相关阅读:
    STM32对HAL库的LCD驱动移植
    stm32对HAL库的DAC使用
    STM32对HAL库的ADC(多通道DMA)
    STM32对HAL库的ADC(单通道非DMA)
    STM32 fputc函数(重定向)
    STM32的HAL库DMA串口不定长度的读写操作(二)
    STM32对HAL库的PWM控制
    STM32对HAL库的定时器中断
    STM32对HAL库的外部中断处理
    C#上位机制作之串口接受数据(利用接受事件)
  • 原文地址:https://www.cnblogs.com/vokie/p/4897107.html
Copyright © 2011-2022 走看看