2012-07-14 85 views
0

我有一个我试图加载数据的TableView。从NSLog显示的数据是正确的,并呈现。然而,下面的方法没有被调用,例如在NSLog注释不会出现在所有:什么触发cellForRowAtIndexPath?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];  
    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
} 
NSString *cellValue = [listOfStates objectAtIndex:indexPath.row]; 
cell.textLabel.text = cellValue; 
return cell; 
NSLog(@"Passed: cellForRowAtIndexPath"); 

}

同样在init没有被任何运行。我虽然init始终运行,如果存在?

- (id)init { 
    if ((self = [super init])) { 
    listOfStates = [[NSMutableArray alloc] init]; 
    stateName = [[NSString alloc] init]; 
    NSLog(@"INIT StateName %@", stateName); 
    } 
return self; 
NSLog(@"Passed: init"); 
} 

任何想法?

回答

1

tableView:cellForRowAtIndexPath:由需要的细​​胞时,其数据源上的表视图调用,这可以是当首次示出的细胞,或当用户滚动和细胞将出现

你的初始化方法没有被调用,因为UITableView使用initWithFrame:style:和UITableViewController(它不清楚你试图覆盖哪个init)使用initWithStyle:,而不是“普通”初始化方法。如果你想要做自定义设置你应该重写这些方法

和其他答案已经提到的,你放东西后返回语句永远不会被执行

+0

非常感谢您的智慧。 initWithFrame或initWithSTyle都没有被激活? 对于数据源,所以我需要把cell.pickerDelegate = self;进入cellForRowAtIndexPath?到目前为止,我总是使用StoryBaord来处理数据源。 并感谢您踢我的退货顺序:-) – 2012-07-14 09:48:01

+0

哪个类的init方法是你重写? pickerDelegate是完全不同的东西,但你确实需要将tableView的dataSource设置为一个对象(如果你使用UITableViewController,这是为你完成的,数据源是控制器) – wattson12 2012-07-14 09:50:18

+0

DataSource和Delegate被链接到TableView中。 – 2012-07-14 11:15:03

1

如果你把NSLog返回后,它会退出方法和NSLog将不会被调用,每当你调用返回它退出该方法。

0

为了回答你的问题:tableView:cellForRowAtIndexPath:在你的表格视图中被称为数据源方法。为了得到它,你必须将tableviews的datasource属性设置为实现tableView:cellForRowAtIndexPath:的类。另一方面sujith是正确的:NSLog将不会在return之后被调用。

+0

事实上,你已经到了该方法正确设置DataSource属性被称为。猜你认为:) :) – HeikoG 2012-07-14 09:06:47

+0

当然 - 修正! – Pfitz 2012-07-14 09:09:41

+0

如上所述,对于如何在此方法中指定DataSource而言天真。你能像通常通过StoryBoard那样为我拼写出来吗?非常感谢。 – 2012-07-14 09:49:36