2012-04-26 4 views
1

두 섹션이있는 UITableView이 있다고 가정합니다. 해당 섹션의 데이터 소스가 비어 있으면 "섹션 이름이 비어있는 자리 표시 자 셀을 표시하려고합니다."UITableView의 자리 표시 자 셀

어떻게하면됩니까?

코드

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    if(section == 1) 
    { 
     return @"Section One"; 
    } 
    if(section == 0) 
    { 
     return @"Section Two"; 
    } 
    return @""; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (section == 1) 
    { 
     return self.sectionOne.count; 
    } 
    else 
    { 
     return self.sectionTwo.count; 
    } 
} 

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

    NSArray *theSource =[[NSArray alloc] init]; 

    if(indexPath.section == 1) 
    { 
     theSource = self.sectionOne; 
    } 
    else 
    { 
     theSource = self.sectionTwo; 
    } 

    // See if there's an existing cell we can reuse 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"]; 
if (cell == nil) 
    { 
     // No cell to reuse => create a new one 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"]; 
     cell.backgroundView = [[UIImageView alloc] init]; 
     // Continue creating cell 
     } 
    } 
+0

이미 가지고있는 코드를 게시하십시오! –

답변

3

당신의 UITableViewDataSource (의사 코드)에 다음과 같은 기능을 구현 :

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (datasource == empty) 
     return 1; 
    else 
     return [datasource count]; 
} 

그리고 :

테이블보기에서 일을하고 오히려 것을
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (datasource == empty) 
     return stub cell; 
    else 
     return regular cell; 
} 
+0

고마워, 잘 했어! –

0

; 나는 데이터 모델을 체크하고 그에 따라 데이터 모델을 업데이트 할 수있다.

섹션 제목 배열에서 섹션 제목을 표시하고 있습니다 (가정).

해당 섹션에 행/레코드가 있는지 여부를 확인할 수 있습니다. 그렇지 않으면 섹션 제목 배열을 적절하게 업데이트하십시오.

sectionTitleArray = [@"First Section", @"Second Section", @"Third Section"] 

rowValueArray = [NSArray를 arrayWithObjects "First2"@ "첫번째 1"] [NSArray를 어레이], [NSArray를 arrayWithObjects @ "Third1"@ "Third3"@]

경우 ([rowValueArray objectAtIndex : 1] 카운트] < 0) {

// 행 번째 섹션 비어; **

는 업데이트 sectionTitleArray

sectionTitleArray = [ "첫 번째 섹션"@ "섹션이 비어", "세 번째 섹션"@]

}

실제 코드가 아닌 단지 시나리오입니다.