2014-10-07 72 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的支持。

相关问题