2012-02-21 9 views
1

私はそれにカスタムビューを持つUITableViewを持っています。 tableviewの一番下には、そこに "Load More"セルを追加します。 クリック後、tableviewはより多くのデータを正常にロードし、 "Load More"セルはテーブルビューの一番下に残ります。 しかし、最初の "Load More"をクリックすると、2番目の "Load More"が表示され、2番目の "Load More"の同じセルにカスタム表示が残ります。 「詳細を読み込む」とカスタム表示が同じセルにあります。私はそのセルが "もっと見る"としか見えないようにしたい。UITableViewカスタムビューと同じセルに「詳細を読み込む」

この問題は、3番目の「4番目の負荷」に存在します。 誰でも助けてくれますか?

- (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];    

この行はにラベルを追加しますセルのコンテンツビューが、そのセルが再使用されたときに、そのセルにラベルがまだ残っています。

if(cell == nil)の後にifとelseの両方で次のコードを追加して、インデックスパスのチェックを行います。

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

ありがとう、私はこれを試しましたが、それは私にEXC_BAD_ACCESSエラーを与えました。 私はこれに2日間苦労しました。 –

+0

変更したコードを更新し、メソッドパスの開始時にNSLogを実行してインデックスパスがクラッシュしていないか確認してください – Shubhank

+0

if(cell == nil)の下にコードを追加しました。その他、質問で更新されました([cell.contentViewサブビューのUIView *ビュー]) ) {[view removeFromSuperview]; } –

関連する問題