2012-02-21 2 views
1

У меня есть UITableView с пользовательским представлением. Для нижней части таблицы, я добавляю туда ячейку «Загрузить еще». После того, как щелкнул, таблица просмотрела больше данных, а ячейка «Загрузить еще» - в нижней части таблицы. Однако после того, как я щелкнул первую «Загрузить больше», появится вторая «Загрузить больше», и пользовательский вид все еще существует в той же ячейке второй «Загрузить больше». «Загрузить больше» и пользовательский вид находятся в одной ячейке. Я хочу, чтобы эта ячейка появилась только «Загрузить больше».Пользовательский вид UITableView и «Загрузить больше» в той же ячейке

Эта проблема существует для третьего, четвертого «Загрузить больше». Может ли кто-нибудь мне помочь?

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
     return imageCurPos+1; 
} 
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if([aTableView tag]==501){ 
     // Main Table 
     static NSString *CellIdentifier = @"Cell"; 
     UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     [[cell viewWithTag:3007] removeFromSuperview]; 

     const NSInteger BOTTOM_LABEL_TAG = 3002; 
     UIImageView *bottomLabel; 
     UILabel *loadMore; 


     if(indexPath.row < imageCurPos){ 
     if (cell == nil) 
     { //All repeat things come here, if don't want to repeat here, please state outside this brucket 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     } 

//Edited 
for (UIView *view in [cell.contentView subviews]) 
    { 
     [view removeFromSuperview]; 
    } 


      bottomLabel = [[[UIImageView alloc] initWithFrame: CGRectMake(50, 30, 250, 112)] autorelease]; 
      [cell.contentView addSubview:bottomLabel]; 

      bottomLabel.tag = BOTTOM_LABEL_TAG; 

      cell.backgroundView = 
      [[[UIImageView alloc] init] autorelease]; 
      cell.selectedBackgroundView = 
      [[[UIImageView alloc] init] autorelease]; 

     UIImageView *iconView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 30, 45, 45)] autorelease]; 
     iconView.image = [UIImage imageNamed:@"02_bubble.png"]; 
     [[cell contentView] addSubview:iconView]; 

     bottomLabel.image = [UIImage imageNamed:@"05_bubble.png"]; 

     }else if(indexPath.row == imageCurPos){ //For Load More 
      if (cell == nil) 
      { //All repeat things come here, if don't want to repeat here, please state outside this brucket 
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
      } 

//Edited 
for (UIView *view in [cell.contentView subviews]) 
    { 
     [view removeFromSuperview]; 
    } 


       loadMore = 
       [[[UILabel alloc] 
        initWithFrame: 
        CGRectMake(0, 0, 300, 50)] 
       autorelease]; 

       loadMore.text = @"Load more..."; 
       loadMore.textAlignment = UITextAlignmentCenter; 

       loadMore.tag = 3007; 

       loadMore.textColor = [UIColor whiteColor]; 
       loadMore.backgroundColor = [UIColor darkTextColor]; 
       [cell.contentView addSubview:loadMore];    
     } 
     return cell;  
    } 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath{ 
if([tableView tag]==501){ 
     if([indexPath row] != imageCurPos){ 

     }else{ // For load more 

      NSLog(@"noRow Prev: %d", imageCurPos); 
      imageCurPos += interval; 
      NSLog(@"noRow After: %d", imageCurPos); 

      [tbl_mo_main reloadData]; 
     } 
} 

ответ

2

Я думаю, вы должны использовать 2 разных идентификатора, один для нормальной ячейки и один для загрузки большей ячейки.

И одна вещь заключается в том, что создание настраиваемой ячейки должно выполняться в блоке.

if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

    // create the views in side the cell and add it 
    ... 
} 

Затем вне этого блока обновите ячейку. Вы можете установить метку для представлений в создании ячейки, и обновить соответствующий вид, загрузив ее с [cell.contentView viewWithTag:tag]

Вы можете обратиться к этой теме: Reload TableViewCell's UILabels in dynamic table

0
 [cell.contentView addSubview:loadMore];    

Эта строка добавляет метку cell cellview, но когда ячейка повторно используется. Эта метка все еще присутствует в этой ячейке.

Добавьте следующий код после if (cell == nil) в обоих случаях, если и для проверки пути индекса.

for (UIView *view in [cell.contentView subviews]) 
    { 
     [view removeFromSuperview]; 
    } 
+0

Спасибо, я попробовал это, но это дало мне ошибку EXC_BAD_ACCESS. Я боролся с этим в течение двух дней. –

+0

обновите свой код с внесенными вами изменениями, а также выполните NSLog в начале метода, чтобы проверить, какой путь указателя указан сбой – Shubhank

+0

Я просто добавил код ниже под (cell == nil), он сработает после нажатия на Load Подробнее, обновлено в Вопросе для (UIView * view в [cell.contentView subviews]) { [просмотреть removeFromSuperview]; } –

Смежные вопросы