1

2つのセクションがある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
        }
  }
4

2 に答える 2

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;
}
于 2012-04-26T19:59:46.447 に答える
0

テーブルビューで何かをするのではなく。私はデータモデルをチェックインし、それに応じてデータモデルを更新する可能性があります。

セクション タイトル配列からセクション タイトルを表示しています (私はそう思います)。

そのセクションの行/レコードが存在するかどうかを確認できます。そうでない場合は、それに応じてセクション タイトル配列を更新します。

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

rowValueArray = [[NSArray arrayWithObjects:@"First1",@"First2"],[NSArray array],[NSArray arrayWithObjects:@"Third1",@"Third3"]

if([[rowValueArray objectAtIndex:1]count]<0){

// 2 番目のセクションの行は空です。**

次に、sectionTitleArray を更新します

sectionTitleArray = [@"第 1 セクション", @"セクションは空です", @"第 3 セクション"]

}

これは単なるシナリオであり、実際のコードではありません。

于 2012-04-26T20:18:53.340 に答える