2012-02-21 43 views
1

我有UITableView自定義視圖。 對於tableview的底部,我在那裏添加一個「加載更多」單元格。 單擊後,tableview會成功加載更多數據,並且「加載更多」單元仍位於tableview的底部。 但是,當我點擊第一個「加載更多」後,出現第二個「加載更多」,並且自定義視圖仍然存在於第二個「加載更多」的同一單元中。 「加載更多」和自定義視圖在同一個單元格上。我希望該單元格只顯示「加載更多」。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 contentview,但是當細胞被重用時,該標籤仍然存在於該細胞中。

在if和else兩個if(cell == nil)之後,在索引路徑檢查中添加以下代碼。

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

謝謝,我試過了,但它給了我EXC_BAD_ACCESS錯誤。 我在這掙了兩天。 – 2012-02-21 03:27:00

+0

更新你的代碼與你所做的更改,也做一個NSLog在方法的開始,以檢查哪個索引路徑崩潰 – Shubhank 2012-02-21 03:36:25

+0

我剛剛添加下面的代碼if(cell == nil),它點擊後加載後崩潰加載更多內容請參考 (對於[cell.contentView子視圖]中的UIView *視圖) { [view removeFromSuperview]; } – 2012-02-21 03:40:33

相關問題