2014-10-07 69 views
-1

我在一個視圖控制器中有兩個不同的tableView,我收到一條錯誤消息。數據源和委託被設置爲視圖控制器。我在tableview方法中做錯了什麼。我以前沒有在同一視圖中處理過多個tableView。由於UITableView DataSource必須從cellforRowAtIndexPath返回一個單元格錯誤

- (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; 

} 
+0

你在哪裏製作細胞?如果在故事板中,您是否給了它們相同的標識符,以便您在此傳遞給出隊方法? – rdelmar 2014-10-07 20:46:51

回答

2

除非你已經呼籲registerClass:forCellReuseIdentifier:或您UITableView定義在你的筆尖或故事板原型類,你會得到nil細胞從dequeReusableCellWithIdentifier:回電。如果您願意,可以註冊一個,或者您可以在收到nil時創建本地實例,請確保將UITableViewCell initWithStyle:reuseIdentifier:作爲您的初始化方法。

+0

只有當單元格不是從筆尖或故事板加載時纔是如此。 – jlehr 2014-10-07 20:42:30

+0

在筆尖或故事板中,我認爲您仍然需要爲重用標識符指定一個原型類。這與調用「registerClass:forCellReuseIdentifier:」基本相同。 – 2014-10-07 20:45:40

+0

確實如此,但您的答案與目前所寫的相同,因此請考慮使用該附加信息進行更新。 – jlehr 2014-10-07 21:28:48

1

我在代碼中看不到任何即時問題。我假設這些單元格在原型單元格中正確註冊或者通過在代碼中註冊它們。如果不是那麼這可能是問題。我會在你的cellForRowAtIndexPath中放置一個斷點:並逐步完成整個過程,以確保它被調用,並確保它實際上正在返回一個單元格。確保所有情況下的單元格都不是零。

-1

您不爲單元分配內存。試試這個代碼:幾個原因中

- (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; 
    } 
} 
1

沒有足夠的信息,而是兩個:

(1)試圖deque的它們之前,你就沒有註冊的兩個自定義單元格。

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

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

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

(2)您使用的是cellForRowAtIndexPath方法標識符名稱不匹配您在viewDidLoad方法進行註冊的。

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

相關問題