- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //標識符 static NSString *iden = @"cardsCell"; CardsCell *cell = [tableView dequeueReusableCellWithIdentifier:iden]; Cards *cards = [self.cardsArr objectAtIndex:indexPath.row]; //cell為空就創建 if (cell == nil) { cell = [[CardsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden]; }else { //如果存在cell需要把之前添加的會變化的控件刪除 //刪除cell上添加的圖片 [cell.photoGroup removeFromSuperview]; } cell.cards = cards;; return cell; }
在自定義cell里
- (void)setCards:(Cards *)cards { if (_cards != cards) { _cards = cards; } //給cell控件賦值 [self.avaImageView sd_setImageWithURL:[NSURL URLWithString:_cards.avaImageUrl] placeholderImage:[UIImage imageNamed:@"72tx.png"]]; self.usernameLabel.text = _cards.username; self.commentLabel.text = [NSString stringWithFormat:@"%ld",_cards.replyArr.count]; self.titleLabel.text = _cards.title; self.timeLabel.text = _cards.time; self.contextLabel.text = _cards.context; //判斷圖片數組數量,大於0就根據數量創建,否則不創建 if (_cards.imageArr.count > 0) { //圖片瀏覽,用了一個圖片瀏覽器SDPhotoGroup,繼承UIView self.photoGroup = [[SDPhotoGroup alloc] initWithFrame:CGRectMake1(0, 100, 105*_cards.imageArr.count, 60)]; NSMutableArray *temp = [NSMutableArray array]; [_cards.imageArr enumerateObjectsUsingBlock:^(NSString *src, NSUInteger idx, BOOL *stop) { SDPhotoItem *item = [[SDPhotoItem alloc] init]; item.thumbnail_pic = src; [temp addObject:item]; }]; self.photoGroup.photoItemArray = [temp copy]; [self.contentView addSubview:self.photoGroup]; } }