2011-08-25 62 views
1

重複使用單元格時,是否可以將標籤添加到UITableViewCell?這是我一直在嘗試的代碼,但是當我向下滾動並且單元格被重用時,我的標籤四處移動。我只是希望標籤始終位於第一排,但我無法想象這是否適合我的生活。我知道我不能重複使用這些單元,它會工作,但我想重用這些單元。請幫我弄清楚這一點。非常感謝...重複使用單元格時向第一個UITableViewCell行添加標籤

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

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier]; 

    if (cell == nil){ 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease]; 

    } 

    if ([indexPath row] == 0) { 

     UILabel* Label = [[UILabel alloc] initWithFrame:CGRectMake(2,2, 62, 23)]; 

     [Label setText:@"Text"]; 

     [cell addSubview:Label]; 

     [Label release]; 
    } 

    return cell; 

} 

回答

3

我想我想通了。我使第一行不可重用。

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

    static NSString *CellsToBeReused = @"CellsToBeReused"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellsToBeReused]; 

    if (cell == nil){ 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellsToBeReused] autorelease]; 
    } 
    if ([indexPath row] == 0) { 
     UITableViewCell *first_Cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease]; 
     UILabel* Label = [[UILabel alloc] initWithFrame:CGRectMake(2,2, 62, 23)]; 
     [Label setText:@"Text"]; 
     Label.backgroundColor = [UIColor whiteColor]; 
     [first_Cell.contentView addSubview:Label]; 
     [Label release]; 
     return first_Cell;   
    } else { 
     return cell;   
    } 
} 
相關問題