2012-02-20 7 views
0

私は非常に単純なテーブルビューを作成し、より多くの機能をロードしました。 "Load More"というテキストの最後のセルにカスタムビューを追加しました ユーザーがload more関数をクリックすると、行が正常に増加しました。 しかし、 "Load More"というテキストは消えませんでした。 助けてください。リロード後もUITableViewカスタムビューがまだここにあります

ここに私のコードです。

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    noRow = 10; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return noRow+1; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (indexPath.row != noRow) { // As long as we haven’t reached the +1 yet in the count, we populate the cell like normal 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Set up the cell... 
    NSString *cellValue = [[NSNumber numberWithInt:[indexPath row]] stringValue]; 

    cell.text = cellValue; 
    } // Ok, all done for filling the normal cells, next we probaply reach the +1 index, which doesn’t contain anything yet 

    else if(indexPath.row == noRow) { // Here we check if we reached the end of the index, so the +1 row 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     } 
     UILabel *loadMore; 

     loadMore =[[UILabel alloc]initWithFrame: CGRectMake(0,0,320,50)]; 
     loadMore.textColor = [UIColor blackColor]; 
     loadMore.highlightedTextColor = [UIColor darkGrayColor]; 
     loadMore.backgroundColor = [UIColor clearColor]; 
     loadMore.font=[UIFont fontWithName:@"Verdana" size:20]; 
     loadMore.textAlignment=UITextAlignmentCenter; 
     loadMore.font=[UIFont boldSystemFontOfSize:20]; 
     [email protected]"Load More.."; 
     [cell addSubview:loadMore]; 


    }   
    return cell; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    if(indexPath.row == noRow){ 
     NSLog(@"noRow Prev: %d", noRow); 
     noRow += 5; 
     NSLog(@"noRow After: %d", noRow); 
     [self.tableView reloadData]; 

    }else{ 
     NSLog(@"IndexPath.row: %d", indexPath.row); 
    } 
} 

答えて

1

あなたは再利用されているとあなたはあなたがセルではないcell.contentViewにサブビューとしてloadMoreを追加する必要があります

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    [[cell viewWithTag:121] removeFromSuperview];//remove this tag view 
    if (indexPath.row != noRow) { // As long as we haven’t reached the +1 yet in the count, we populate the cell like normal 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     } 

     // Set up the cell... 
     NSString *cellValue = [[NSNumber numberWithInt:[indexPath row]] stringValue]; 

     cell.text = cellValue; 
    } // Ok, all done for filling the normal cells, next we probaply reach the +1 index, which doesn’t contain anything yet 

    else if(indexPath.row == noRow) { // Here we check if we reached the end of the index, so the +1 row 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     } 
     UILabel *loadMore; 

     loadMore =[[UILabel alloc]initWithFrame: CGRectMake(0,0,320,50)]; 
     loadMore.textColor = [UIColor blackColor]; 
     loadMore.highlightedTextColor = [UIColor darkGrayColor]; 
     loadMore.backgroundColor = [UIColor clearColor]; 
     loadMore.font=[UIFont fontWithName:@"Verdana" size:20]; 
     loadMore.textAlignment=UITextAlignmentCenter; 
     loadMore.font=[UIFont boldSystemFontOfSize:20]; 
     [email protected]"Load More.."; 
     loadMore.tag = 121;// just setting the tag 
     [cell addSubview:loadMore]; 


    }   
    return cell; 
} 
+0

ありがとうございました。 この問題を抱えている人は、上記のコードの中に.tagを付けた2行を追加するだけです。 –

1

より負荷のために追加したビューを削除されていません。その後

あなたcellForRowにこの行を追加します。..

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

現在、より多くのあなたの負荷が再利用および再利用場合、その後の細胞中に存在しているされています。

関連する問題