簡體   English   中英

UITableView數據源必須從cellforRowAtIndexPath錯誤返回一個單元格

[英]UITableView DataSource must return a cell from cellforRowAtIndexPath Error

我在一個視圖控制器中有兩個不同的tableViews,但收到一條錯誤消息。 數據源和委托設置為視圖控制器。 我在tableview方法中做錯了嗎? 在同一視圖中,我沒有處理過多個tableView。 謝謝

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
if (tableView == self.postsTableView) {
    return 1;
}
else if (tableView == self.eventsTableView){
return 1;
}
return 1;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (tableView == self.postsTableView) {
    return 1;
}
else if (tableView == self.eventsTableView){
    return 1;
}
return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if ([tableView isEqual: self.postsTableView]) {
    profilePagePostCell *cellOne = (profilePagePostCell *) [tableView dequeueReusableCellWithIdentifier:@"profilePostCell"];
    cellOne.postLabel.text = [NSString stringWithFormat:@"Hi"];
    return cellOne;
}

if ([tableView isEqual: self.eventsTableView]) {
    profileEventCell *cellTwo = (profileEventCell *) [tableView dequeueReusableCellWithIdentifier:@"profileEventCell"];
    cellTwo.eventLabel.text = [NSString stringWithFormat:@"The big One"];
    return cellTwo;
}

profileEventCell *cell = (profileEventCell *) [tableView dequeueReusableCellWithIdentifier:@"profileEventCell"];
return cell;

}

除非您已經調用registerClass:forCellReuseIdentifier:或在UITableView筆尖或情節registerClass:forCellReuseIdentifier:定義了原型類,否則將從dequeReusableCellWithIdentifier:調用中獲取nil單元格。 您可以根據需要注冊一個實例,也可以在收到nil創建一個本地實例,請確保將UITableViewCell initWithStyle:reuseIdentifier:稱為初始化方法。

我沒有在您的代碼中看到任何直接的問題。 我假設這些單元格已在原型單元格中或通過在代碼中注冊而正確注冊。 如果沒有,那可能就是問題所在。 我將在您的cellForRowAtIndexPath中設置一個斷點,並逐步執行該過程以確保它甚至被調用為一個,並確保它實際上正在返回一個單元格。 確保在所有情況下該單元格都不為零。

信息不足,但有以下兩個原因:

(1)在嘗試將兩個自定義單元格配對之前,您尚未注冊它們。

如果是這種情況,請在覆蓋viewDidLoad同時注冊它們,如下所示。

[self.postsTableView registerClass:[profilePagePostCell class] forCellReuseIdentifier:@"profilePostCell"];

要么

[self.postsTableView registerNib:@"YOUR_NIB_NAME" forCellReuseIdentifier:@"profilePostCell"]

(2)您在cellForRowAtIndexPath方法中使用的標識符名稱與您在viewDidLoad方法中注冊的名稱不匹配。

仔細檢查名稱,強烈建議您使用定義的常量名稱,以便獲得Xcode的支持。

您不為單元分配內存。 試試這個代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath(NSIndexPath *)indexPath{

    static NSString *cellIdentifierPosts = @"cellIdentifierPosts"
    static NSString *cellIdentifierEvents = @"cellIdentifierEvents"

    if ([tableView isEqual: self.postsTableView]) {
        profilePagePostCell *cellOne = (profilePagePostCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifierPosts];
        if (!cellOne)
            cellOne = [[profilePagePostCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: cellIdentifierPosts];
        cellOne.postLabel.text = [NSString stringWithFormat:@"Hi"];
        return cellOne;
    }

    else if ([tableView isEqual: self.eventsTableView]) {
        profileEventCell *cellTwo = (profileEventCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifierEvents];
        if (!cellTwo)
            cellTwo = [[profileEventCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: cellIdentifierEvents];
        cellTwo.eventLabel.text = [NSString stringWithFormat:@"The big One"];
        return cellTwo;
    }
    else{
        profileEventCell *cell = (profileEventCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifierEvents];
        if (!cell)
            cell = [[profileEventCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: cellIdentifierEvents];
        cell.eventLabel.text = [NSString stringWithFormat:@"The default One"];
        return cell;
    }
}

暫無
暫無

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

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