1

UITableView2 つの動的セルでグループ化された情報を表示しようとしています。グループ化された各UITableViewビューを 10 回呼び出す必要があります。それぞれがグループ化UITableViewされ、その 2 つのセル間で異なる情報が表示されます。

現在、データベースに保存されているすべてのデータを表示しようとしていますposts。ただし、このバージョンを実行すると、アプリがクラッシュします。returnを return[self.posts count]に変更すると、予測どおり、1 つのセクションのみをロードできます。numberOfSectionsInTableView:1

私の質問は、それぞれが異なる情報を持つ 10 個のグループ化されたテーブル セクションを表示するにはどうすればよいですか?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
// Return the number of sections.
return [self.posts count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{


NSInteger returnValue = 2;
switch (section) {
    case 0:
    {
        returnValue = 2;
        break;
    }
    default:
        break;
}

return returnValue;

}


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

NSString *CellIdentifier1 = @"Cell";


UITableViewCell *cell;
if (indexPath.section==0)
{
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
    if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
    }

    if (indexPath.row==0)
            {
                cell.textLabel.text = [[self.posts objectAtIndex:indexPath.row] objectForKey:@"name"];
            }
    if (indexPath.row==1)
            {
                cell.textLabel.text = [[self.posts objectAtIndex:indexPath.row] objectForKey:@"message"];
            }
}

return cell;

}
4

2 に答える 2

1

section = 0のときにセルを作成しています。毎回セルを作成する必要があります。cellForRowAtIndexPathメソッドから nil を返すことはできません。

以下の方法で実装を変更します。

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

    NSString *CellIdentifier1 = @"Cell";        
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];

    if (cell == nil)
    {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
    }

    if (indexPath.section==0)
    {
        if (indexPath.row==0)
        {
            cell.textLabel.text = [[self.posts objectAtIndex:indexPath.row] objectForKey:@"name"];
        }
        //Rest of the code..
    }
    else if(indexPath.section ==1)
    {
        //Do the work with section 1.
    }
    //do the work for other sections...

    return cell;
}
于 2013-04-24T04:02:30.827 に答える
0

書かれているコードはあなたの状況に十分です。if (indexPath.section==0){のこの行を削除するだけcellForRowAtIndexPathです。

更新:あなたの指摘によると、次のコードで十分です。正しく保存した場合posts

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

NSString *CellIdentifier1 = @"Cell";


UITableViewCell *cell = nil;

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
    if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
    }

    if (indexPath.row==0)
            {
                cell.textLabel.text = [[self.posts objectAtIndex:indexPath.section] objectForKey:@"name"];//Here modified `section` instead of `row`
            }
    else if (indexPath.row==1)
            {
                cell.textLabel.text = [[self.posts objectAtIndex:indexPath.section] objectForKey:@"message"];
            }


return cell;

}
于 2013-04-24T04:05:28.500 に答える