簡體   English   中英

UITableView tableView:cellForRowAtIndexPath:沒有被調用

[英]UITableView tableView:cellForRowAtIndexPath: not getting called

我在一個視圖中有3個表視圖,我想知道為什么未調用tableView:cellForRowAtIndexPath:

 #pragma mark -
 #pragma mark <UITableViewDelegate>
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
    if (tableView == self.mainVoucherTableViewController)
       [self setSelectedIndex:indexPath.row];
 }

 - (NSInteger)tableView:(UITableView *)tableView
   numberOfRowsInSection:(NSInteger)section
 {
    if (tableView == self.mainVoucherTableViewController){
  return 10;
     }
 }

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
    if (tableView == self.mainVoucherTableViewController){
    static NSString *MyIdentifier = @"MyReuseIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:MyIdentifier];
    }

    cell.textLabel.text = @"THISTEXT";
    return cell;
   }
}


 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

 if (tableView == self.mainVoucherTableViewController)
     return 1;
 }

 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// The header for the section is the region name -- get this from the region at the section index.
if (tableView == self.mainVoucherTableViewController){
    NSString * myString = [NSString stringWithFormat:@"HELLLO WORLD"];
    return myString;
  }
}

有人會知道為什么嗎? 基本上,這不會創建任何單元格或顯示單元格。 它僅顯示表視圖。 :(

首先在tableView:cellForRowAtIndexPath:方法內部登錄,以查看是否在if語句之外以及內部都調用了該方法,以幫助縮小問題范圍。

另外,請嘗試不要比較您的:

tableView == self.mainVoucherTableViewController

將tableViews設置為具有標簽值。 然后,您可以執行以下操作:

if(tableView.tag == 100){ // tag number we assigned self.mainVoucherTableViewController via IB
    //do your stuff here
}

只是為了整合以上幾點:

  1. 從您的命名來看,您的tableView稱為mainVoucherTableViewController-僅想確認這確實是UITableView而不是UITableViewController? 如果它是一個UITableViewController,那么由於明顯的原因(或者不是那么明顯-告訴我並可以進一步解釋),其余的這些將無法正常工作

  2. 確保使用以下代碼或在Interface Builder中將當前viewController設置為tableView的委托和dataSource(如果使用的是XIB)

     self.mainVoucherTableViewController.delegate = self; self.mainVoucherTableViewController.dataSource = self; 
  3. 確保您的numberOfRowsInSection函數正在被調用,並且您返回的非零值(放入NSLogs等),並且對numberOfSections也執行相同的操作(實際上,如果僅使用1個部分,則不需要numberOfSections)-請參閱UITableViewDataSource協議參考: http : //developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html

  4. 根據先前的帖子,在開始時以及返回之前記錄您的cellForRow(如果檢查了第1-3點並正常工作)以確保它已被觸發。 還要對要返回的單元格執行NSLog,以確保其不為零。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
     if (tableView == self.mainVoucherTableViewController)
    {
       return 10;
     }
      else
     {retun 5;
     }
   }

它在第一張表中顯示10行,第二張表中顯示5行。

實例聲明的順序確實很重要。 例如,如果您有一個名為tableView的ivar:

錯誤

self.tableView.delegate = self;
self.tableView = [UITableView alloc] init];

正確

self.tableView = [UITableView alloc] init];
self.tableView.delegate = self;

檢查UITableView對象框架大小。 可能幀大小不足以繪制Cell。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM