1

我对 TableView 有一个奇怪的问题。这是一些澄清一切的代码:

我正在使用选项卡式窗格,这只是我的两个 ViewController 之一:

如您所见,我所做的只是设置一个 UIView 并在其上放置一个 tableView。

#import "SuchenCtrl.h"

@interface SuchenCtrl ()

@end

@implementation SuchenCtrl

@synthesize MainView;
@synthesize Standort;

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.
    self.view = self.MainView;

    [self.Standort.tableView setDelegate:self.Standort];
    [self.Standort.tableView setDataSource:self.Standort];

    [self.MainView addSubview:self.Standort.tableView];
}

- (id)init {
    self = [super init];

    if(self) {
        CGRect rect = [[UIScreen mainScreen] applicationFrame];
        self.MainView = [[UIView alloc] initWithFrame:rect];
        [self.MainView setBackgroundColor:[UIColor blackColor]];

        //Alloc UITableViewController
        self.Standort = [[UIStandort alloc] initWithStyle:UITableViewStylePlain];

        [self.Standort release];
        [self.MainView release];
    }

    return self;
}

@end

现在 UIStandort 是另一个简单地从 UITableViewController 继承的类。它实现了显示数据所需的所有功能。我试图实现的只是让表格显示“测试”几次,但它不会。表格显示得很好,但只有空单元格。当我断点 UIStandort 类中已实现的函数之一时,什么都没有被调用,所以我的假设是委托和 sourceData 指向错误的类?不过,我在将表格放在视图上之前明确分配了这些。

如果有帮助,UIStandort* Standort 被声明为具有属性 retain 和 nonatomic。

#import "UIStandort.h"

@interface UIStandort ()

@end

@implementation UIStandort

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

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

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

    UITableViewCell* cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.textLabel.text = @"Test";

    [cell autorelease];

    return cell;
}

@end

谢谢。

4

1 回答 1

3

您返回 0 表示节中的行数。该文档指出:

tableView 中的节数。默认值为 1。

你能试一下吗:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}
于 2012-09-23T22:27:42.273 回答