0

通常の UIViewController で次のコードを他のコンテンツとともに使用しました。テーブル ビューはビューの下部にあります。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 // Return the number of rows in the section.
    return [names count];
}

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

    // Configure the cell...
    NSString *cellValue = [names objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;

    return cell;
}

そして名前と呼ばれるNSMutableArray(4つのオブジェクトを持つ)

4

3 に答える 3

2

テーブル ビューのデータ ソースデリゲートを UIViewController に設定し、両方のプロトコルに必要なメソッドを実装する必要があります。

于 2012-08-19T17:16:49.857 に答える
0

ビューコントローラーをテーブルビューとして設定するのを忘れている可能性がありますdataSource(UITableViewControllerこれは自動的に行われます)。

于 2012-08-19T17:14:12.027 に答える
0

このコードは、UITableViewController でも機能しません... でテーブル ビュー セルを作成する必要があります-tableView:cellForRowAtIndexPath:。セルをデキューするとき、デキューできるセルがない場合、戻り値は nil になることがあります。

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

    // Create the cell if it could not be dequeued
    if (cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    // Configure the cell...
    NSString *cellValue = [names objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;

    return cell;
}
于 2012-08-19T17:16:14.660 に答える