簡體   English   中英

UITableViewController不會調用cellForRowAtIndexPath但是numberOfSectionsInTableView和numberOfRowsInSection確實設置得當

[英]UITableViewController will not call cellForRowAtIndexPath BUT numberOfSectionsInTableView and numberOfRowsInSection are indeed set properly

#import "AssignmentsViewController.h"
#import "Assignment.h"

@interface AssignmentsViewController ()

@property NSMutableArray *assignments;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation AssignmentsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    NSLog(@"viewDidLoad");
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.assignments = [[MySingleton sharedMySingleton] assignments];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

- (void)viewWillAppear:(BOOL)animated {
    NSLog(@"viewWillAppear");
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"numberOfRowsInSection");
    return [self.assignments count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"cellForRowAtIndexPath");
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AssignmentCell"];
    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AssignmentCell"];
    }
    cell.textLabel.text = [[self.assignments objectAtIndex:indexPath.row] title];
    return cell;
}

@end

加載AssignmentsViewController並且至少有一個家庭作業在assignments數組中時,控制台輸出:

2013-11-06 18:55:09.396 Homework Planner[4756:70b] viewDidLoad
2013-11-06 18:55:09.403 Homework Planner[4756:70b] numberOfSectionsInTableView
2013-11-06 18:55:09.405 Homework Planner[4756:70b] numberOfRowsInSection
2013-11-06 18:55:09.408 Homework Planner[4756:70b] viewWillAppear
2013-11-06 18:55:09.409 Homework Planner[4756:70b] numberOfSectionsInTableView
2013-11-06 18:55:09.409 Homework Planner[4756:70b] numberOfRowsInSection

我正在創建一個顯示用戶家庭作業的應用程序。 由於某種原因,即使我確定賦值數組中有一個家庭作業分配對象,也從不調用cellForRowAtIndexPath。 通過模態視圖將作業分配添加到數組中,並且我已經驗證它們確實是由該過程添加的,並且在用戶點擊“取消”之后視圖返回到由該類定義的視圖。 當發生這種情況時,另外兩個與UITableView相關的方法運行,但不是cellForRowAtIndexPath。 請幫我解決這個問題。

讓我再提出一個罪魁禍首:“內存管理問題”。

如果TableView的DataSource對象沒有保留(保留計數為零)而TableView正在從中提取數據,那么,令人驚訝的是,盡管numberOfSections和numberOfRows剛剛被調用,但不會調用tableView:cellForRowAtIndexPath:

檢查[self.assignments count]是否返回0(檢查self.assignments是否nil

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"[self.assignments count] = %d", [self.assignments count]);
}

否則,我現在能想到的另一個原因是,當委托方法返回的高度為0時(如果你正在實現它,否則默認實現總是給出非零高度,所以在這種情況下沒有問題。)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  CGFloat height = 0;
  // Some calculation to determine height of row/cell.

  return height;
}

如果cellForRowAtIndexPath沒有調用它,因為視圖尚未在屏幕上呈現(未繪制),因此系統無法知道Cell或UITableView的大小。

要解決該問題,請確保在視圖之后添加項目(如果已呈現)(並且已知尺寸)。 如果在父視圖中添加UITableView以構建復雜屏幕,則會發生這種情況。

如果在初始化階段未加載數據,則可以在設置數據后調用reloadData。

例:

OfferTableViewController * offerList = [[OfferTableViewController alloc] init];

// add the view in the parent view to make sure that the dimension are calculated
[fullSiteView.offersView addSubview: offerList.view]; 
offerList.delegate = self;
// Set the data
offerList.offers = product.offers;
// Force reload to render
[offerList.tableView reloadData];

此外,如果覆蓋heightForRowAtIndexPath,請確保它返回> 0的內容

請注意,即使numberOfRowsInSection和numberOfSectionsInTableView返回正確的項目數,如果表的大小未知,系統也不會調用cellForRowAtIndexPath

另一個問題可能是tableview本身的框架大小為0或屏幕外...

我被困了多年,看着numberOfRowsInSection的日志被調用和heightForRowAtIndexPath:被調用並返回一個硬編碼值,但是從未調用過cellForRowAtIndexPath:!

嘗試注銷tableView的框架,或在其上設置背景顏色以檢查其實際可在屏幕上查看。

暫無
暫無

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

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