1

我在 UIView 中创建了一个 UITableView,只使用代码(不使用 IB)

 m_tableData = [[UITableView alloc] init];
 m_tableData.dataSource = self; 
 m_tableData.delegate = self;   
 [self addSubview:m_tableData];

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     NSLog(@"%d", [arrPanelListImg count]);
     return [arrPanelListImg count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"kCell"];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"kCell"] ;


        NSLog(@"%d", indexPath.row);

    }

return cell;

}

在日志屏幕中我看到

numberOfRowsInSection: 方法返回 18。而 cellForRowAtIndexPath: 方法,只需运行 9 次(适合屏幕)。

而且我在我的代码中没有发现任何奇怪的地方。所以在这种情况下,有人对我有任何建议。先谢谢了。

4

2 回答 2

0

您需要使用UITableView'initWithFrame:style:方法 - 这是UITableView该类的指定初始化程序。

于 2012-09-18T18:55:22.553 回答
0

你的意思是

m_tableData = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];

当我改变它时,与我原来的结果没有什么不同。真的很奇怪,这是我第一次看到这个错误,仍然没有解决办法。

编辑: - 只需删除检查条件单元 == nil 并正常工作。

于 2012-09-19T16:12:02.150 回答