2012-10-07 2 views
1

XIB에서 사용자 지정 셀을로드하는 UITableView를 만들려고합니다. 나는 EventsCell.xib를 .m 및 .h 파일과 함께 가지고있다.동적 콘텐츠가있는 사용자 지정 UITableViewCell

셀의 내용과 높이는 알 수없는 수의 하위보기를 포함 할 수 있으므로 동적입니다.

이것은 셀을로드하는 코드입니다.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *CellIdentifier = @"EventsCellIdentifier"; 

     EventsCell *cell = (EventsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil) 
     { 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"EventsCell" owner:self options:nil]; 
      cell = [nib objectAtIndex:0];   
     } 

     NSDictionary *hour = [[[eventsArray objectAtIndex:[indexPath section]] valueForKey:@"hours"] objectAtIndex:[indexPath row]]; 

     NSArray *events = [hour valueForKey:@"events"]; 

     cell.events = events; 
     cell.timeLabel.text = [hour valueForKey:@"name"]; 

     int offset = 0; 
     for(NSDictionary *event in events) 
     { 
      UIView *vvv = [[UIView alloc] initWithFrame:CGRectMake(50, offset, 270, 44)]; 
      [vvv setBackgroundColor:[UIColor clearColor]]; 

      UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)]; 
      lab.text = [event valueForKey:@"name"]; 
      lab.backgroundColor = [UIColor clearColor]; 

      [vvv addSubview:lab]; 

      [cell.contentView addSubview:vvv]; 

      offset += 44; 
     }  

     return cell; 
    } 

이 세포를로드,하지만 난 테이블 뷰에 몇 번을 스크롤 한 후, 내용은 다음과 같습니다 :

enter image description here

내가 뭘 잘못 ..

감사합니다!

==================== UPDATE ====================

이제 또 다른 일이 일어나고 있습니다. 셀에 추가 한 하위 뷰의 모든 요소를 ​​별도의보기 컨트롤러에 추가했습니다. 노호처럼.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *CellIdentifier = @"EventsCellIdentifier"; 

     EventsCell *cell = (EventsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil) 
     { 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"EventsCell" owner:self options:nil]; 
      cell = [nib objectAtIndex:0];   
     } 

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

     NSDictionary *hour = [[[eventsArray objectAtIndex:[indexPath section]] valueForKey:@"hours"] objectAtIndex:[indexPath row]]; 

     NSArray *events = [hour valueForKey:@"events"]; 

     cell.events = events; 
     cell.timeLabel.text = [hour valueForKey:@"name"]; 

     int offset = 0; 

     for(NSDictionary *event in events) 
     { 
      EventBoxViewController *ebvc = [[EventBoxViewController alloc] initWithNibName:@"EventBoxViewController" bundle:nil]; 
      ebvc.nameLabel.text = [event valueForKey:@"name"]; 

      [ebvc.view setFrame:CGRectMake(50, offset, 270, 44)]; 
      [cell.contentView addSubview:ebvc.view]; 

      offset += 44; 
     }  

    return cell; 
} 

그리고로드하지 않아도됩니다. 아래 이미지를 참조하십시오. 난 세포를 스크롤 할 때

enter image description here

그들은 단지로드가 정상적으로 심지어 그들은로드되지 않습니다. 또한 레이블 텍스트가 지정되지 않습니다. 내가 뭘 잘못하고 있는지에 대한 아이디어가 있습니까?

감사합니다.

답변

4

뷰는 세포가 reuable 때문에 여전히 우리 모두가 파단을 제거하면 그것이

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

if (cell == nil) 
{ 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"EventsCell" owner:self options:nil]; 
    cell = [nib objectAtIndex:0];   
} 
+0

정말 고마워요! :) 그것은 나를 미치게했다. –

+0

질문을 한 번 더 업데이트했습니다. 한 번 봐 주시겠습니까? –

+0

UIViewController 유형의 EventBoxViewController입니까? 그렇다면 프레임을 설정하더라도 기본 치수를 취할 것입니다 (이것은 여기에서 문제가 될 수 있음). UIView로 변경/상속 할 수 있습니까? 그것은 작동해야합니다. 또한 행의 높이가 고정되거나 가변적입니까? 변수가 있으면 코드를 입력하십시오. 어떻게 계산합니까? – D25

1
후 위의 코드를 추가 작동, 추가 유지, cell.contentView에 추가

테이블 셀 프로토 타입에 UIVIew *를 추가하고 하위 뷰를 지우면 작동합니다. 이 같은 :

UIView* editView = (UIView*)[cell viewWithTag:102]; 
for (UIView *subV in [editView subviews]){ 
     [subV removeFromSuperview]; 
0

더 많은 읽을 수있는 솔루션은 하나마다 시간을 만드는 대신 셀을 dequeueing이다, 그러나 당신이 때문에 오버 헤드의 끝 부분에로드 할 필요가 얼마나 그들 중 많은 따라 달라집니다. 이 제거하는 것은 더러운 것처럼 보입니다.

관련 문제