2012-02-20 26 views
0

我做了一個非常簡單的表視圖,加載更多的功能。 我在文本「加載更多」的最後一個單元格上添加了自定義視圖。 用戶單擊加載更多功能後,行成功增加。 但文本「加載更多」沒有消失。 請幫忙。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

正在重用的,你是不是刪除已添加的負載更

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

謝謝,它有幫助。 對於有此問題的其他人,只需在上面的代碼中添加2行.tag即可。 – 2012-02-20 09:30:47

1

你應該添加loadMore作爲一個子視圖到cell.contentView不是細胞的視圖。

後添加在您的cellForRow此行..

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

目前您的負載更多的是被重用,如果重複使用在隨後的細胞存在。