2012-01-18 4 views

답변

15

테이블보기 맨 위에 이미지보기를 추가하거나 테이블보기의 배경보기를 변경할 수 있습니다. 당신에

// Check if table view has any cells 
int sections = [self.tableView numberOfSections]; 
BOOL hasRows = NO; 
for (int i = 0; i < sections; i++) { 
    BOOL sectionHasRows = ([self.tableView numberOfRowsInSection:i] > 0) ? YES : NO; 
    if (sectionHasRows) { 
     hasRows = YES; 
     break; 
    } 
} 

if (sections == 0 || hasRows == NO) 
{ 
    UIImage *image = [UIImage imageNamed:@"test.png"]; 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 

    // Add image view on top of table view 
    [self.tableView addSubview:imageView]; 

    // Set the background view of the table view 
    self.tableView.backgroundView = imageView; 
} 
+0

고마워, 좋은 속임수. 그런데 어떻게 UIView를 시각적으로 디자인하고이를 테이블의 레이어로 사용할 수 있습니까? 더 이상 새로운 XCode 스토리 보드로 뷰를 디자인 할 수있는 옵션이 없습니다. – bashan

+0

새 nib 파일을 만들고 UIView에로드하여 뷰를 디자인 할 수 있습니다. – simonbs

+0

이전 XCode에서 사용했습니다. 스토리 보드와 함께 제공되는 새로운 XCode는 다른 방식으로 작동하는 것 같습니다. 새로운 사용자 정의 크기보기를 만들 수 없으며 모든보기가 동일한 스토리 보드 파일에 있습니다. – bashan

4

UITableViewbackgroundView 속성을가집니다. 이 속성을 배경 이미지가 포함 된 UIImageView으로 설정합니다.

+0

0보다 큰 내가 jQuery과에 대한 배경을 보여주고 싶지 않아,주의해야합니다. 표에 셀이없는 경우에만 배경을 표시하려고합니다. 나는 단순히 테이블을 숨기고 대신 배경을 넣기를 원합니다. 당신의 제안이 그 일을 할 수 있습니까? – bashan

0

numberOfRowsInSection 방법 :

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    if data.count > 0 { 

     return data.count 
    } 
    else{ 

     let image = UIImage(named: "Nature") 
     let noDataImage = UIImageView(image: image) 
     noDataImage.frame = CGRect(x: 0, y: 0, width: tableView.bounds.width, height: tableView.frame.height) 
     tableView.backgroundView = noDataImage 
     tableView.separatorStyle = .none 

     return 0 
    } 
} 

numberOfSections

관련 문제