2013-07-12 54 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() 
}) 
相關問題