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;
}

还有一个名为 names 的 NSMutableArray(有 4 个对象)

4

3 回答 3

2

您需要设置表格视图的数据源委托给您的 UIViewController,并为这两种协议实现所需的方法。

于 2012-08-19T17:16:49.857 回答
0

您可能忘记将视图控制器设置为表格视图dataSourceUITableViewController自动执行此操作)。

于 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 回答