2012-02-24 25 views
2

我已經從我身邊搜索了很多研究,並找出了爲什麼tableview上的reloadData方法不起作用。我檢查了所有可能的解決方案,如數據源設置,代理設置,tableview連接到文件的所有者。uitableView reloadData在設置委託,數據源和文件的所有者連接後不起作用

畢竟這些,當我試圖重新加載tableview,沒有。行方法被調用,但rowAtIndexPath單元格不會被調用。以下是我寫的代碼。請讓我知道,我要去的地方錯了

- (void)onReservationListSuccess:(NSArray *)rData 
{ 
if (rData != nil) 
{ 
    resList = [[NSArray alloc] initWithArray:rData]; 

    if([resList count] > 0) 
    { 
     [self.tripsTableView reloadData]; 
     //[self.tripsTableView beginUpdates]; 
     //[self.tripsTableView reloadSections:[NSIndexSet indexSetWithIndex:0] 
      //   withRowAnimation:UITableViewRowAnimationNone]; 
     //[self.tripsTableView endUpdates]; 
    } 
    else 
    { 
     [tripsTableView reloadData]; 
     [tripsTableView setHidden:YES]; 
     [noTripsLabel setHidden:NO]; 
    } 
} 

if(fsnNeedsRefresh == YES) 
{ 
    [[NSNotificationCenter defaultCenter] postNotificationName:UpdateFSNList object:nil]; 
    fsnNeedsRefresh = NO; 
} 
} 


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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
int temp=[resList count]; 
NSLog(@"The no. of rows are %d", temp); 
NSLog(@"Testing Purpose"); 
NSLog(@"The pnr details of the object is:%@",((TripData *)[resList objectAtIndex:0]).pnrDescription); 
return 1; 
} 


// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"The cell for the row at indexpath is getting called"); 
static NSString *CellIdentifier = @"TripCellIdentifier"; 

TripCell *cell = (TripCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
{ 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TripCell" owner:self options:nil]; 

    for(id oneObject in nib) 
     if([oneObject isKindOfClass:[TripCell class]]) 
      cell = (TripCell *)oneObject; 
} 

// Set up the cell... 
TripData *tripData = (TripData *)[resList objectAtIndex:indexPath.row]; 
cell.pnrLabel.text = tripData.pnr; 
NSLog(@"The cell text is %@",tripData.pnr); 
cell.pnrDescriptionLabel.text = tripData.pnrDescription; 
NSLog(@"The cell text is %@",tripData.pnrDescription); 
cell.pnrTypeLabel.text = tripData.pnrType; 
NSLog(@"The cell text is %@",tripData.pnrType); 

if(checkInAllowed) 
{ 
    cell.checkInButton.tag = indexPath.row; 
    [cell.checkInButton addTarget:self action:@selector(checkIn:) forControlEvents:UIControlEventTouchUpInside]; 
} 
else 
{ 
    [cell.checkInButton setEnabled:NO]; 
} 

return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
// Navigation logic may go here. Create and push another view controller 
TripData *tripData = (TripData *)[resList objectAtIndex:indexPath.row]; 

NSLog(@"%@", tripData.pnr); 

if(tripData != nil) 
{ 
    TripOverviewViewController *tripOverviewViewController = [[TripOverviewViewController alloc] initWithTrip:tripData]; 
    [self.navigationController pushViewController:tripOverviewViewController animated:YES]; 
    [tripOverviewViewController release]; 
} 

[tableView deselectRowAtIndexPath:indexPath animated:NO]; 
} 

回答

3

從代碼這一部分,我不能準確說出它爲什麼不工作,但我會盡力解釋如何reloadData作品。

首先,UITableView如何工作:基本上,它是一個滾動視圖。當它被繪製時,它會檢查它有多少行,然後檢查它們的高度,從它的大小和滾動位置決定當前顯示哪些行。然後它要求代表爲每個顯示的行返回一個UITableViewCell。 滾動表格時,會從視圖層次結構中刪除隱藏的單元格,並添加已出現的單元格。

而現在棘手的部分 - reloadData做什麼?它只是從表層次結構中刪除所有UITableViewCell。而已。當表格在reloadData之後第一次繪製時,實際更新完成。

所以,我的建議是 - 檢查您的表是不是隱藏並檢查其框架。另外,我看到你正在訪問屬性獲得者self.tripsTableView和ivar tripsTableView。這很混亂。他們都回來了嗎?

+0

非常感謝您的解釋。我確實將上面的行更改爲[tripsTableView reloadData]和dint查看任何更改。我調試並發現,在第一種方法中,我將一些結果存儲在數組rData中,並且reloadData方法調用no。行,我可以從rData打印內容。但它不是調用cellForRowIndexPath,如果我滾動tableview,數組rdata中根本沒有數據。 – jeevangs 2012-02-24 21:06:49

+0

我看到的唯一解釋是表格認爲沒有行應該顯示。你從numberOfSections返回1,從numberOfRowsInSection返回1,所以應該總是顯示1行。 rowHeight屬性設置是否正確?不是由scrollview插入隱藏的單元格? – Sulthan 2012-02-24 21:19:23

+0

你是對的。只有1個單元格必須顯示,並且單元格可見。沒有問題。我唯一擔心的是,indexpath中的行的單元格沒有正確調用。無論如何,我會試圖找出我要去哪裏錯了。如果你遇到,請告訴我。 – jeevangs 2012-02-24 21:43:08

相關問題