2011-08-25 3 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

나는 생각했습니다. 나는 1 행을 재사용 할 수 없게 만들었다.

- (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;   
    } 
} 
관련 문제