recherche

Maison  >  Questions et réponses  >  le corps du texte

ios - iphone UITableViewCell 重复的问题?

因为 cell会复用,所以使用cell的时候会检查一下cell的状态,比如


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cartCell" forIndexPath:indexPath];

if(cell == nil){
    //do something
    UIImageView *imageView = alloct init
    imageView.tag = tag;
    [cell.contentView addSubview:imageView]
}
else{
    UIImageView *imageView = cell from tag get view
}

但是我的情况是,cell一开始就不为空,于是我这么做:

 if ([cell.contentView subviews].count == 0) {
     // do some thing
 }
 
 

最后我发现,一个table有5个cell全部都在显示区域里面,只有第一个cell是新的,其他都是复用的,这样是不是不对?

讲道理不应该是屏幕内的cell都是独立的,当屏幕外的cell滑动进来才会导致复用么?

高洛峰高洛峰2888 Il y a quelques jours419

répondre à tous(4)je répondrai

  • 伊谢尔伦

    伊谢尔伦2017-04-18 09:18:22

    D'accord, Cool Ai Dian, j'aimerais ajouter :

    - (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);

    Cette méthode renverra une cellule non vide par défaut. S'il n'y a pas de cellule pouvant être directement réutilisée, elle vous aidera à créer une cellule, donc l'approche générale est la suivante :

    1. Créez une sous-classe ACell de UITableViewCell

    2.ACell méthode de réécriture :

    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
        if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
            // configure cell
        }
    }

    Cette méthode sera appelée lorsque la sortie de file d'attente ne trouve pas de cellule réutilisable et doit créer une nouvelle cellule

     ;

    3. Enregistrez cette classe dans tableView (registerNib peut également être utilisé) :

    [tableView registerClass:[ACell class] forCellReuseIdentifier:identifier]

    4. Appelez-le directement lorsque cellForRowAtIndexPath :

    ACell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    [cell setModel:model];

    répondre
    0
  • 怪我咯

    怪我咯2017-04-18 09:18:22

    Il est recommandé de réutiliser le code en héritant de UITableViewCell. Et initialisez la sous-vue dans - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier.

    -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
        if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
            self.selectionStyle = UITableViewCellSelectionStyleNone;
    
      

    répondre
    0
  • 阿神

    阿神2017-04-18 09:18:22

    if ([cell.contentView subviews].count == 0) {

     // do some thing

    } Ce jugement ne sera jamais vrai, la cellule est livrée avec quelques sous-vues

    répondre
    0
  • PHP中文网

    PHP中文网2017-04-18 09:18:22

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cartCell"];
    }
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.tag = tag;
    [cell.contentView addSubview:imageView]

    }

    répondre
    0
  • Annulerrépondre