2013-02-02 2 views
0

barView 저는 테이블 뷰를 가지고 있으며 각 셀에 통계 막대를 그립니다. 내 사용자 지정 셀에 빈 UIView이 만들어지면 해당 이름은 mainView이며 차트가 그려지는보기입니다. cellForRowAtIndexPath에서UIView addSubview 또는 UITableViewCell에서 작업하지 않습니다.

나는이 일을 해요 :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"CustomCellIdentifier"; 
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    CGRect barRect = CGRectMake(0, 0, 100, 10); 

    UIView *barView = [[UIView alloc] initWithFrame:barRect]; 
    [barView setBackgroundColor:[UIColor redColor]]; 
    [cell.mainView addSubview:barView]; 

    return cell; 
} 

barView로드에 표시되지 않습니다. 셀이 화면 밖으로 나간 후에 다른 탭 또는 에서 변경하고 돌아온 후에 만 ​​표시됩니다..

참고 : 나는

  • [cell.mainView setNeedsDisplay];을 시도

    • 나는 다른 사용자 정의 세포로 만든 헤더를 가지고있다.
  • +0

    UITableViewCell에서 mainView를 추가하는 방법은 무엇입니까? – CRDave

    +0

    CustomCell이라는 사용자 정의 클래스를 작성하고 mainView라는 IBOutlet 특성을 추가했습니다. 그런 다음 클래스를 셀에 할당하고 IBOutlet을 interfaceBuilder의 뷰에 할당했습니다. – Octan

    +0

    'initView '란 무엇입니까? 대신에'barView'를 사용 했습니까? 그렇지 않으면 빈보기 만 만들었 기 때문에 아무 것도 볼 수 없다는 것이 분명합니다. – SAE

    답변

    -1

    (따라서 만 if (!cell)에서 그것을 만들기) 다시 추가 없습니다 :

    뷰가 생성됩니다 전에 하위 뷰를 추가하는 것은 할 수 못할 것으로 보인다
    - (void)viewDidAppear:(BOOL)animated 
    { 
        [self.tableView reloadData]; 
    } 
    

    창에서 셀이 완전히로드되기 전에

    0

    귀하의 설명에 따르면 귀하는이 기능 밖에서 막대보기를 채우고 있음을 분명히 나타냅니다. 따라서 전환 후에 만 ​​보이게됩니다. 또는 여기에서 제공 한 것 외에 단순히 덮어 쓰거나 (또는 ​​메인 뷰를 덮어 쓰는 것조차도).

    viewdidload 및 viewdidappear와 같은 것을 확인하십시오.

    +0

    고맙지 만'barView' 또는'mainView'가'cellForRowAtIndexPath' 함수 외부에 있지 않습니다. – Octan

    1

    이 시도 :

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
        static NSString *CellIdentifier = @"CustomCellIdentifier"; 
        CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    
        if (!cell) { 
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
         CGRect barRect = CGRectMake(0, 0, 100, 10); 
         UIView *barView = [[UIView alloc] initWithFrame:barRect]; 
         [barView setBackgroundColor:[UIColor redColor]]; 
         [cell addSubview:barView]; // use cell.mainView if you have that created somewhere other than here (like IB) 
        } 
    
        return cell; 
    } 
    

    dequeueResuable...이 전무 셀을 반환 할 수 있습니다. 따라서 새 셀을 할당/초기화해야하는지 확인해야합니다. dequeueResuable...이 올바른 셀을 반환하면 내가 준 코드에서 이미 서브 뷰가 그대로 만들어 졌을 것입니다. 그래서 필요가 쉽게 나는 그것을 해결 한

    +0

    고맙습니다. 그러나 이것은 내 문제를 해결하지 못합니다. (귀하의 코드를 시도했습니다) 내 셀은 항상 올바르게 만들어지고 레이블과 내용은 모두 올바르게로드되고 값은 정상입니다. 'cell.customLabel.text = ...'를 사용하면 모든 것이 OK입니다. 'cell.mainView'도 OK입니다. 그것은 인터페이스 빌더에서 만든 뷰이고'[cell.mainView setBackgroundColor : ...] '로 색상을 변경할 수 있습니다.이 모든 것이'mainView' 안에 새로운 뷰를 그리는 것처럼 보입니다. 그리고 그것을 해결 한 방법은'viewDidAppear'에'tableView' 데이터를 다시로드하는 것입니다. 고맙습니다. – Octan

    관련 문제