2013-07-12 55 views
4

我被困在一个愚蠢的问题,因为两天。我有UITableViewController推入Navigation Controller。当它加载,因为没有数据,所以空表可见:UITableviewcontroller中的UITableview - cellforrowatindexpath在调用时未被调用

但是,当我收到来自服务器的数据,并调用[self.tableView reloadData],既numberOfRowsInSectionheightForRowAtIndexPath得到调用除了cellForRowAtIndexPath,我的控制器显示无表:

我真的不能明白为什么它正在发生。除了cellForRowAtIndexPath之外,所有数据源方法都被调用。请人指导我...谢谢..

ActLogController.h

@interface ActLogController : UITableViewController<ASIHTTPRequestDelegate,UITableViewDataSource,UITableViewDelegate> 

@property(strong) NSMutableArray *activityKeys; 

@end 

ActLogController.m

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    activityKeys = [[NSMutableArray alloc] init]; 
    self.tableView.dataSource = self; 
} 

-(void) viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    [self retrieveActivityLogFromServer]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return activityKeys.count; 
} 

- (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]; 
    } 
    // Configure the cell... 
    ActivityLogUnit *act = [activityKeys objectAtIndex:indexPath.row]; 
    cell.textLabel.text = act.price; 

    return cell; 
} 

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 50.0; 
} 

-(void) requestFinished:(ASIHTTPRequest *)request 
{ 
    NSArray *list = [request.responseString JSONValue]; 

    for (int i = 0; i < list.count; i++) { 

     NSArray *singleTrade = [[list objectAtIndex:i] JSONValue]; 

     ActivityLogUnit *unit = [[ActivityLogUnit alloc] init]; 

     unit.symbol = [singleTrade objectAtIndex:0]; 
     unit.type = [singleTrade objectAtIndex:1]; 
     unit.price = [singleTrade objectAtIndex:2]; 
     unit.volTraded = [singleTrade objectAtIndex:3]; 
     unit.remVol = [singleTrade objectAtIndex:4]; 
     unit.actualVol = [singleTrade objectAtIndex:5]; 
     unit.recordID = [singleTrade objectAtIndex:6]; 
     unit.orderNo = [singleTrade objectAtIndex:7]; 
     unit.time = [singleTrade objectAtIndex:8]; 

     [activityKeys addObject:unit]; 

    } 

    if(activityKeys.count > 0) 
    { 
     [self.tableView reloadData];//it is called and I have got 6 items confirm 
    } 
} 

编辑

我设置一些虚拟数据在我的阵列activityKeys,Da ta被显示在表格中,并且cellforrowatindexpath被成功调用。但随着我在某段时间后重新加载数据,其他方法被调用,除了这一个,表格消失,如第二张图所示。有任何想法吗?

+0

你有IBOutlet中连接了的tableView? –

+0

在numberOfRowsInSection中记录activityKeys.count以查看它返回的内容。 – rdelmar

+0

@Bhargavi我没有得到我的tableviewcontroller的xib文件。 – NightFury

回答

0

最后我得到了它的工作。 cellForRowAtIndexPath没有被调用,因为我没有在这里提到的一行代码......这实际上是从视图中删除一些颜色背景层。这是导致重新加载的问题。删除后,一切正常。

谢谢大家的合作:)

4

你在viewDidLoad中

self.tableView.dataSource = self; 
self.tableView.delegate = self; 

编辑

写你没有那么厦门国际银行在您的声明/设置你的tableview的属性。像

self.tableView.frame = CGRectMake(0, 45, 320, 500); 
self.tableView.rowHeight = 34.0f; 
self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone; 
[self.view addSubview:self.tableView]; 

[self.tableView setBackgroundColor:[UIColor clearColor]]; 
self.tableView.showsVerticalScrollIndicator=NO; 
self.tableView.dataSource = self; 
self.tableView.delegate = self; 

@property(nonatomic,strong) NSMutableArray *activityKeys; 
+0

我已经写了..没用。 – NightFury

+0

看到我编辑的答案 –

+0

我没有xib,但仍然最初显示表(第1张图),并且也将其分配给它。我也试过你的答案,但它并没有帮助我:( – NightFury

1

尝试首先,我坚信的tableview的实例名称不应该是类似于局部变量(即的tableView类应该不等于的tableView的代表和数据源方法)。

第二次在您发布的问题中,我看不到为表视图设置的委托。 回答发布者Samir Rathod应该可以在你的.h或.m文件中设置了表视图@property的情况下工作。

如果您有XIB文件,也可以执行此操作。 按住Ctrl并单击并拖动tableview到文件所有者并设置委托和数据源。

+0

所有的第一次,我没有有任何实例。其次,我试着用xib和设置插座,但仍然结果是一样的。 – NightFury

+0

你记录了数组的数量吗?我知道你上面提到过它。但是如果这样的话,任何高于0的计数都应该调用cellForRowAtIndex。可能有一个很小的步骤,你必须失踪。尝试再次诊断您的步骤。 –

5

你的问题是,你可能会在后台线程上下载数据内容。由于您无法在后台更新UI,所以一旦下载完成,您需要在主线程上调用[self.tableView reloadData]!

希望它有帮助!

+0

我没有使用任何线程...感谢您的想法:) – NightFury

+1

你的答案帮助了我!我的修复是使用performSelectorOnMainThread重载数据。 – brians

5

看起来你在辅助线程,通过使用下面的代码

[self.tableView [email protected](reloadData) withObject:nil waitUntilDone:NO] 

您可以随时使用[NSThread isMainThread]检查你是否在主线程还是不做reloadData在主线程。

+0

感谢您的回答。它不工作。我仍然得到空白观点:( – NightFury

+0

作为一个全面的检查,你确定的cellForRowAtIndexPath不获取调用?断点.. .. NSLog的是 –

+0

我都用了,并检查它不被调用。 – NightFury

0

我也有同样的症状。就我而言,我第一次在viewDidLoad中加载数据(来自核心数据)时,使用NSSortDescriptor对数据进行排序。

点击一个按钮后,核心数据再次被提取(这次更改)并重新载入tableView数据。它最初给我一个空白表视图,点击按钮后,因为我第二次提取它时忘记排序数据。

学习要点:如果您在开始时已经使用过它们,请记住调用修改单元格的所有方法(如iAnum提及的背景颜色,或者NSSortDescriptor)。

1

对我来说,问题在于我的残段代码返回0作为段的数量(所以它从来没有问过段中有多少行,并且从来没有得到他们的数据)。如果这也是您的问题,请将其更改为1。另外,我在斯威夫特,其中通过@沙希德拉希德提到的问题正在被编码(略)是不同的:

dispatch_async(dispatch_get_main_queue(), { 
    self.tableView.reloadData() 
}) 
相关问题