UITableView未从UIView填充



我遇到了这个问题。我放在UIView中的tableView没有填充,或者有时会出现异常。

// .h
@interface List : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
    UITableView *mTableView;
}
@property (nonatomic, readonly) IBOutlet UITableView *tableView;
//------------------------------
// .m
@synthesize tableView=mTableView;
//Further I do the normal viewDidUnload stuff = nill
//And the numberOfSectionsInTableView, numberOfRowsInSection and 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"cellForRowAtIndexPath:");
    NSLog(@"Table View: %@",[tableView description]);
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = @"Hi";
    // Configure the cell...
    return cell;
}

我得到的输出:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<List 0x6a78000> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key tableView.'

或者只是一个空单元格的列表,甚至没有"嗨"作为标题。

我几乎可以肯定,发生此错误是因为您未正确连接UITableView。您应该删除ivar mTableView、IB出口属性tableView和@synthesize。然后删除IB中的出口。然后,通过从接口生成器中拖动它来再次连接它,并且不要在类中键入任何代码。Xcode会为你做所有的事情(创建一个属性,合成它并做MM的事情)

确保已在Interface Builder 中设置委托和表的数据源

如果您不使用IBOutlet做任何事情,为什么要使用它?方法

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

由在nib中创建的tableView实例的nib文件调用。您的类作为此实例的委托接收此调用。在笔尖中成为tableView的代理就是你所需要的。

最新更新