2014-09-03 4 views
-1

tableView에는 두 가지 종류의 셀 레이아웃이 있습니다. 하나는 일반적인 높이, 다른 하나는 확장 된 높이입니다. 두 가지 모두 XIB에서 만들어졌습니다. 문제는 확장 된 셀이 데이터를 덮어 쓰는 것입니다. 예를 들어 레이블은 다른 셀을 확장 할 때 이전 텍스트 위에 추가되는 텍스트로 설정됩니다.표보기 셀 덮어 쓰기 데이터

다음은 셀이로드되는 방식입니다.

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

    static NSString *cellIdentifier = @"Cell"; 
    static NSString *expandedCellIdentifier = @"ExpandedCell"; 
    if (!isExpanded) 
    { 
     ListCell *cell =(ListCell*) [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
     if (cell==nil) 
     { 
      NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"ListCell" owner:self options:nil]; 
      cell = nibs[0]; 
     } 
     cell.Name.text = [[bArray objectAtIndex:indexPath.row]valueForKey:@"Name"]; 
     return cell; 
    } 
    else 
    { 
     expCell =(ExpandedCell*) [tableView dequeueReusableCellWithIdentifier:expandedCellIdentifier]; 
     if (expCell==nil) 
     { 
      NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"ExpandedCell" owner:self options:nil]; 
      expCell = nibs[0]; 
     } 
     UILabel *jTime = [[UILabel alloc]initWithFrame:CGRectMake(27, 6, 72, 10)]; 
     jTime.text = [NSString stringWithFormat:@"%@",timeRequired]; 
     [expCell.background_View addSubview:jTime]; 

     return expCell; 
    } 
    return nil; 
} 

답변

2

셀을 다시 사용할 때마다 레이블이 추가됩니다. 이를 방지하기 위해 if 조건의 else 부분에이 코드를 사용

expCell = (ExpandedCell*) [tableView dequeueReusableCellWithIdentifier:expandedCellIdentifier]; 
if (expCell == nil) 
{ 
    NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"ExpandedCell" owner:self options:nil]; 
    expCell = nibs[0]; 
    UILabel *jTime = [[UILabel alloc]initWithFrame:CGRectMake(27, 6, 72, 10)]; 
    [jTime setTag:100]; //or any value 
    [expCell.background_View addSubview:jTime]; 
} 
UILabel *jTimeLabel = (UILabel *)[expCell.background_View viewWithTag:100]; 
jTimeLabel.text = [NSString stringWithFormat:@"%@",timeRequired]; 

return expCell; 

를이 코드를 통해 레이블은 한 번만 추가 얻을 것이다.

+0

고마워, -] – iSD

1

대기열에서 빠져 나올 때마다 expcell에 새 하위보기가 추가됩니다. 때문에이 라인의

+0

다음 해결 방법 : -] – iSD

+0

nib에서 셀을로드하는 경우, 거기에 서브 뷰를 넣는 것이 어떻습니까? – Kreiri

+0

필자는 programatically.Thats해야 할 몇 가지 드로잉 물건은 이런 식으로 물건을 사용하는 이유. – iSD

0

: 매번 셀이 재사용 새로운 UILabel의 추가됩니다

UILabel *jTime = [[UILabel alloc]initWithFrame:CGRectMake(27, 6, 72, 10)]; 
jTime.text = [NSString stringWithFormat:@"%@",timeRequired]; 
[expCell.background_View addSubview:jTime]; 

.